I used code from this site, which updates the title of a web part, so the change needed is perhaps minor.

The code that isn't working:

Write-Output $webPartDefinition.WebPart.Properties.FieldValues.Content
$webPartDefinition.WebPart.Properties.FieldValues.Content = $statusReport
$webPartDefinition.CloseWebPart() #didn't solve the problem but also didn't seem to hurt
Write-Output $webPartDefinition.WebPart.Properties.FieldValues.Content

the "Write-output" sections show differences before and after, meaning that the contents are successfully read and updated within PowerShell, but the changes just aren't updated on the SharePoint site.

My full 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 = "[email protected]" 
 $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)
 #use the WebPartManger to load the webparts on the page
 $limitedWebPartManager = $page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
 #loop through all WebParts to get the correct one and change its property
 foreach($webPartDefinition in $limitedWebPartManager.webparts){
     #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
         Write-Output $webPartDefinition.WebPart.Properties.FieldValues.Content

1 Answer 1


The answer appears to be that this is not possible:


Testing by a Microsoft employee respondent confirms my findings. This may be possible with the snap-in, though I am not running PowerShell on the SharePoint server, so this does not appear to be an option for me.

I think the next best option is to delete and create the web part each time I want to refresh it. This seems heavy handed to me, hopefully MSFT solves this in future iterations.

Note: Content Editor display the data poorly and is not a good choice for my case.

Here is my extremely verbose solution:

Function replaceScriptEditor(){
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.ClientRuntimeContext] $ctx,
        [Parameter(Mandatory=$true)] [string] $pageRelativeUrl,
        [Parameter(Mandatory=$true)] [string] $contentTitle,
        [Parameter(Mandatory=$true)] [string] $content,
        [Parameter(Mandatory=$true)] [int] $wpZoneOrder

    $wpZoneID = "Main"

    $WebPartXml = [xml] "
      <webPart xmlns='http://schemas.microsoft.com/WebPart/v3'>
          <type name='Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c' />
          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
            <property name='ExportMode' type='exportmode'>All</property>
            <property name='HelpUrl' type='string' />
            <property name='Hidden' type='bool'>False</property>
            <property name='Description' type='string'>Allows authors to insert HTML snippets or scripts.</property>
            <property name='Content' type='string'>$content</property>
            <property name='CatalogIconImageUrl' type='string' />
            <property name='Title' type='string'>$contentTitle</property>
            <property name='AllowHide' type='bool'>True</property>
            <property name='AllowMinimize' type='bool'>True</property>
            <property name='AllowZoneChange' type='bool'>True</property>
            <property name='TitleUrl' type='string' />
            <property name='ChromeType' type='chrometype'>None</property>
            <property name='AllowConnect' type='bool'>True</property>
            <property name='Width' type='unit' />
            <property name='Height' type='unit' />
            <property name='HelpMode' type='helpmode'>Navigate</property>
            <property name='AllowEdit' type='bool'>True</property>
            <property name='TitleIconImageUrl' type='string' />
            <property name='Direction' type='direction'>NotSet</property>
            <property name='AllowClose' type='bool'>True</property>
            <property name='ChromeState' type='chromestate'>Normal</property>
        Write-Host "Getting the page with the webpart we are going to modify" -ForegroundColor Green

        #Using the params, build the page url
        Write-Host "Getting the page with the webpart we are going to modify: " $pageRelativeUrl -ForegroundColor Green

        #Getting the page using the GetFileByServerRelativeURL and do the Checkout
        #After that, we need to call the executeQuery to do the actions in the site
        $page = $ctx.Web.GetFileByServerRelativeUrl($pageRelativeUrl)
        Write-Host "The page is checked out" -ForegroundColor Green

        #Get the webpart manager from the page, to handle the webparts
        $webpartManager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);

        #Load and execute the query to get the data in the webparts
        Write-Host "Getting the webparts from the page" -ForegroundColor Green
        #Remove the existing Script Editor WebPart
        foreach($webPartDefinition in $webpartManager.webparts){
            #send the request containing all operations to the server
                write-host "Error: $($_.Exception.Message)" -foregroundcolor red
            #Only change the webpart with a certain title
            if ($webPartDefinition.WebPart.Properties.FieldValues.Title -eq $contentTitle)
                try {
                    Write-Host "Deleting existing webpart." -ForegroundColor Green
                } catch {  
                    Write-Output $Error

        #Import the webpart
        $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)

        #Add the webpart to the page
        Write-Host "Add the webpart to the Page" -ForegroundColor Green
        $webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder)
            Write-Host "Errors found:`n$_" -ForegroundColor Red

            #CheckIn the Page
            Write-Host "Checkin  the Page" -ForegroundColor Green
            $page.CheckIn("Add the User Profile WebPart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)          

            Write-Host "The webpart has been added" -ForegroundColor Yellow             

        Write-Host "Errors found:`n$_" -ForegroundColor Red


$statusReport = "<![CDATA[$($htmlData]]>"

$tenantAdmin = "[email protected]" 
$tenantAdminPassword = "5ecureP@ss!"

$secureAdminPassword = $(convertto-securestring $tenantAdminPassword -asplaintext -force)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword)  
$ctx.Credentials = $credentials

$contentTitle = 'Data_Health'
[int]$index = 1 #position on page, lower number = higher on page
$relUrl = "/teams/myTeam/Team_Home/Lists/My_List/Gantt_View.aspx"

Write-Output "$(Get-Date -Format yyyy-MM-dd.hh:mm:ss): Updating '$contentTitle' WebPart on site '$relUrl'."
replaceScriptEditor -ctx $ctx -pageRelativeUrl $relUrl -contentTitle $contentTitle -content $statusReport -wpZoneOrder $index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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