Showing posts with label SharePoint Online Update ListItem. Show all posts
Showing posts with label SharePoint Online Update ListItem. Show all posts

Tuesday, 30 April 2019

SharePoint Online Update List item

SharePoint Online Update Rich Text Column html

We had a business requirement to remove/update the list items by removing unnecessary styles added at list item

Below is the code snippet which will loop through all the items and removes  strings  ex: data between style tags i.e all the styles between  <style> </style>

cls
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"

$UserName= "username@companyname.com"
$Password = "pwd"
$credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$webURL="https://companyname.sharepoint.com/teams/sitename/"
$ctx= New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
$ctx.Credentials = $credentials
$list = $ctx.web.Lists.GetByTitle("Resources")
$listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ctx.load($listItems)
$ctx.executeQuery()

foreach($item in $listItems)
{

     try
    {
    $startIndex=$item["Overview_x0020_Text0"].IndexOf("<style>")
    $endIndex=$item["Overview_x0020_Text0"].IndexOf("</style>")
    If($startIndex -gt  0)
    {

    $newitem=$item["Overview_x0020_Text0"].Remove($startIndex,$endIndex+8)
    $item["Overview_x0020_Text0"]=$newitem
    $item.Update();
    Write-Host -ForegroundColor DarkGreen $item.Id   "Item Updated"
    }
     }
     catch
     {
       Write-Host -ForegroundColor Red  $item.Id "Item Not Updated"

     }
}

    $ctx.ExecuteQuery()