Help! Need to update a massive number of hyperlinks to new ones across entire SharePoint Online tenant

Pascoe, Todd 1 Reputation point
2023-03-23T15:32:57.7266667+00:00

Just found out that in less than a week, redirects we have in place with an external vendor will stop working. Which means we've got thousands of links to externally hosted videos across SharePoint, both in lists and on site pages that will stop working.

I have all of the information available in front of me in .csv - current link, new link it should be changed to, site owner, site URL, site page URL, list name (if applicable), hyperlink text, etc. so on and so forth.

Rather than try to run a communication campaign of sending emails to the site owners to update, I'd much rather automate this behind the scenes. Especially since we have hardly any time left. I'm not a SharePoint admin, but I can get any suggestions in front of them. I'm thinking that surely there must be some way of doing this with a PowerShell script. PLease help Community! If you have a solution, you may just save my life (or at least significantly decrease stress level) :)

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,746 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,101 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,681 Reputation points Microsoft Vendor
    2023-03-24T06:04:03.5133333+00:00

    Hi @Pascoe, Todd

    You can refer to following script to update link in csv

    #Connect to SharePoint Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    
    #Import and read the rows one by one
    Import-CSV "C:\xie\SPurl.csv" | ForEach-Object { 
     
    #Current row object
    $CSVRecord = $_
     
    #Read column values in the current row 
    $SiteURL = $CSVRecord.'SiteURL'
    $ListName = $CSVRecord.'ListName'
    $ColumnName = $CSVRecord.'ColumnName'
    $OldLink = $CSVRecord.'OldLink'
    $NewLink = $CSVRecord.'NewLink'
     
    #Get All List Items
    $ListItems = Get-PnPListItem -List $ListName -PageSize 2000
     
    #Iterate through all items in the list
    ForEach ($Item in $ListItems)
    {
        $Flag =  $false
        $URL = $Item.FieldValues[$ColumnName].URL.ToLower()
     
        #Check if the URL has the old text
        If($URL.contains($OldString))
        {
            $URL = $URL -Replace $OldString,$NewString
            $Flag =  $True
        }
     
        #check the description of the Hyperlink
        $Description = $Item.FieldValues[$ColumnName].Description.ToLower()
        If($Description.contains($OldString))
        {
            $Description = $Description -Replace $OldString,$NewString
            $Flag =  $True
        }
     
        #update the Hyperlink field if URL or Description has the old string
        If($Flag -eq $True)
        {
            #Frame the URL field value
            $Hyperlink = New-Object Microsoft.SharePoint.Client.FieldUrlValue
            $Hyperlink.Url= $URL
            $Hyperlink.Description= $Description
     
            #Update Hyperlink field value
            Set-PnPListItem -List $ListName -Identity $Item.Id -Values @{$ColumnName = [Microsoft.SharePoint.Client.FieldUrlValue]$Hyperlink} | Out-Null
            Write-host -f Green "List Item $($Item.ID) Updated!"
        }
    }
    }
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.