0

I have custom list in one site collection which have multi line field (RichText Field). It have images path for that site collection.

Now when I migrate this list to another site collection. Path for the images is still showing old site collection. So I thinking to update those path.

I was thinking to use PowerShell command to update those path. Kindly let me know any other easy way to do that.

Please give me sample PowerShell command to update list item in my scenario

0

The following PowerShell script for your reference.

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteUrl = "https://test.sharepoint.com/sites/LZ2"
$UserName="lz@test.onmicrosoft.com"
$Password ="***"
$ListName="TestUpdateItems"
$FieldName="MultipleText"
$OldSitePath="sites/LZ1"
$NewSitePath="sites/LZ2"

#Setup Credentials to connect
$ctx = new-object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

$list = $ctx.Web.Lists.GetByTitle($ListName)

$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View><RowLimit>200</RowLimit></View>"

do
{
    $started = Get-Date
    $items = $list.GetItems($query)
    $ctx.Load($items)
    $ctx.ExecuteQuery()

    # It is important to update ListItemCollectionPosition of object $query with current position
    $query.ListItemCollectionPosition = $items.ListItemCollectionPosition

    if ($items.Count -eq 0) { break }

    for ($i = 0; $i -lt $items.Count; $i++)
    {
        # update columns as needed
        $items[$i][$FieldName]=$items[$i][$FieldName] -replace $OldSitePath,$NewSitePath
        $items[$i].Update()
    }

    $ctx.ExecuteQuery()
    Write-Host "Time elapsed: $((Get-Date) - $started)"
    # ListItemCollectionPosition is null if there is no other page
} while ($query.ListItemCollectionPosition -ne $null)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.