This PowerShell script can help when you need to change a Web Part property in SharePoint for multiple sites. We have used my script to create around 300 sites using a custom template. Certain sites were already in production when we found an issue with a certain Web Part. In stead of changing this manually I have created a script to loop through all sites and change a certain Web Part property. The script that I’ve created does the following:
- Loop through all subwebs from a given start point
- Get the specific .aspx page
- Get the correct Web Part
- Change the property
The below example will change the property of a specific Web Part on the default /SitePages/Home.aspx
Running the script
We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.
You will need to change the first variables in the Change-WebPart function to match your Office 365 tenant and copy this bit to PowerShell.
function Change-WebPart { #variables that needs to be set before starting the script $siteURL = "https://spfire.sharepoint.com" $userName = "mpadmin@spfire.onmicrosoft.com" $webURL = "https://spfire.sharepoint.com" $relativePageUrl = "/SitePages/Home.aspx" # Let the user fill in their password in the PowerShell window $password = Read-Host "Please enter the password for $($userName)" -AsSecureString</pre> # set SharePoint Online credentials $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password) # Creating client context object $context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL) $context.credentials = $SPOCredentials #get Page file $page = $context.web.getFileByServerRelativeUrl($relativePageUrl) $context.load($page) #send the request containing all operations to the server try{ $context.executeQuery() } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor red } #use the WebPartManger to load the webparts on a certain page $webPartManager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $context.load($webPartManager.webparts) #send the request containing all operations to the server try{ $context.executeQuery() } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor red } #loop through all WebParts to get the correct one and change its property foreach($webPartDefinition in $webpartmanager.webparts){ $context.Load($webPartDefinition.WebPart.Properties) #send the request containing all operations to the server try{ $context.executeQuery() } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor red } #Only change the webpart with a certain title if ($webPartDefinition.WebPart.Properties.FieldValues.Title -eq "Documents") { $webPartDefinition.webpart.properties["Title"] = "My Documents" $webPartDefinition.SaveWebPartChanges() } } } Change-WebPart
First enter your password and press enter (I didn’t add output on this script so trust me that I didn’t had any errors 🙂 )
The above script has changed
To
This script is not very useful when changing only 1 web part but image you need to change a certain web part on 200 project sites because of a template update or mistake. This way you can update all your project sites matching the changes you have made in the template regarding web parts.