3

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?

  • 1
    Im not sure, but I came across a blog the other day searching for something else, he had to solve the issue client side instead: tricky-sharepoint.blogspot.no/2016/07/… – Anders Aune Aug 2 '16 at 13:04
  • That is disconcerting and way more than I want to do, if that is the case, then I'll bite the bullet and configure it through the UI properly instead of introducing all that. – Eric Alexander Aug 2 '16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.