I've created a "replaceScriptEditor" function. Shouldn't be necessary.
Unable to update ScriptEditor WebPart (CSOM, PowerShell)
n8 mmmmmm
1
Reputation point
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.