1

This is a follow-up to my earlier question, How to get all properties of a Site Page using REST API?. Using REST in an AngularJS application, I was able to read the properties of the Web Part Page Title Bar and see if the Image property was populated. Now I realize I have to EDIT the property as well.

Can I use the REST API to edit a web part? Or do I need to use CSOM? I have used CSOM in the past to edit the User Information List (because that's one list you can't edit with REST). But I am not sure how to proceed.

1 Answer 1

1

Interesting use case, I am not sure if we are able to update via REST API but with CSOM you definitely can update Below

Below is using Powershell CSOM, but same can be used in C# or Javascript object model.

function Change-WebPart {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com"
    $userName = "[email protected]"
    $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()
        }
    }
}

Also sharing some other links for quick referece

https://www.sharepointfire.com/2016/04/editing-web-part-properties-with-powershell-csom-in-sharepoint/

https://sharepointsamurai.wordpress.com/2014/07/27/how-to-use-the-csom-to-update-sharepoint-web-part-properties/

below is using PnPcore https://www.c-sharpcorner.com/article/working-with-web-part-properties/

Your Answer

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

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