Unable to update ScriptEditor WebPart (CSOM, PowerShell)

n8 mmmmmm 1 Reputation point
2021-10-07T18:50:52.32+00:00

I am unable to change the contents of the script editor web part, here is my code:

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"

$statusReport = "Last updated: $(Get-date)"
$contentTitle = 'Script_Title'

$webURL = "https://domain.sharepoint.com/teams/team_1/team1_Home"
$relativePageUrl = "/teams/team_1/team1_Home/SitePages/Status_Health.aspx"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)

$username = "nate@domain.com" 
$password = "Super5ecure!" 
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials

$page = $ctx.Web.GetFileByServerRelativeUrl($relativePageUrl)
$ctx.load($page)
$ctx.executeQuery()

#use the WebPartManger to load the webparts on the page
$limitedWebPartManager = $page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$ctx.load($limitedWebPartManager.webparts)
$ctx.executeQuery()

#loop through all WebParts to get the correct one and change its property
foreach($webPartDefinition in $limitedWebPartManager.webparts){

    $ctx.Load($webPartDefinition.WebPart.Properties)
    $ctx.executeQuery()

    #Only change the webpart with a certain title
    if ($webPartDefinition.WebPart.Properties.FieldValues.Title -eq $contentTitle)
    {
        Write-Output $webPartDefinition.WebPart.Properties.FieldValues.Content
        $webPartDefinition.WebPart.Properties.FieldValues.Content = $statusReport
        #$webPartDefinition.WebPart.Properties.FieldValues["Content"] = $statusReport
        $webPartDefinition.SaveWebPartChanges()
        $webPartDefinition.CloseWebPart()
        $ctx.executeQuery()
        Write-Output $webPartDefinition.WebPart.Properties.FieldValues.Content
    }
}

No errors are thrown, and the write-output sections produce the current contents of the script editor and the attempted contents, so the code is definitely reading the web part, it just isn't updating the SP site. I've also tested the DeleteWebPart() function and it works.

I'm at a loss as to what else I can try.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,697 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,382 questions
{count} votes

1 answer

Sort by: Most helpful
  1. n8 mmmmmm 1 Reputation point
    2021-10-13T17:58:35.29+00:00