I am trying to create a script to modify several web parts in a site collection so I don't have to go through the tediousness of doing it 50 times through the browser, but I seem to be running into a problem or an unsettable field in the code. The script looks like this:
function Change-WebPart {
#variables that needs to be set before starting the script
$WebUrl = "https://tenant.sharepoint.com/sites/dev"
$relativePageUrl = "/sites/dev/doclib/Forms/EN.aspx"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
#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("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()
$webPartDefinition.WebPart.Properties.FieldValues
#this dumps all the data fine
}
catch{
write-host "Error: $($_.Exception.Message)" -foregroundcolor red
}
#Only change the webpart with a certain title
if ($webPartDefinition.WebPart.Properties.FieldValues.Title -eq "Controlled Documents")
{
$webPartDefinition.webpart.properties.FieldValues["InplaceSearchEnabled"] = $true
#this has no effect
$webPartDefinition.SaveWebPartChanges()
}
}
}
Change-WebPart
Is this property just not settable or am I approaching it wrong?