Search code examples
restsharepointpower-automateweb-parts

Sharepoint REST API GetLimitedWebPartManager method returns empty result


I would like to edit WebParts of a Sharepoint site with Sharepoint REST API. The best option I found so far is to use the GetLimitedWebPartManager method at first to request all WebParts of a site. At the moment Im working with MS PowerAutomate to design this workflow and therefore I request all results of my API-calls as JSON format.

I found this command which should do it:

https://[domain].sharepoint.com/sites/[dev]/_api/web/getFileByServerRelativeUrl('/sites/Example.aspx')/getLimitedWebPartManager(scope=1)/WebParts?$expand=WebPart/Properties

Unfortunately, the response was as following (even tho the site is not empty and has 3 WebParts):

{
  "d": {
    "results": []
  }
}

Also tried something like the following commands, but I think Im still missging something here.

https://[domain].sharepoint.com/sites/[dev]/_api/web/getFileByServerRelativeUrl('/sites/Example.aspx')/getLimitedWebPartManager()/WebParts?$expand=WebPart

https://[domain].sharepoint.com/sites/[dev]/_api/web/getFileByServerRelativeUrl('/sites/Example.aspx')/getLimitedWebPartManager()/WebParts?$select=*&$expand=WebPart

Has anyone an idea what could be causing the empty result?


Solution

  • First of all, I assume you use the modern interface and not the classic one.

    As Expiscornovus said you may take a look at the new graph apis related to webparts.

    However, this api is limited to some well known webparts.

    If you need more control, you can play with the CanvasContent1 field of the page. This field contains a JSON representation of the page, describing the sections and the webparts inside each section.

    I saw power-automate tag in your question. Web build in our company a provisioning software inside power automate. It was a tricky project since power automate is very limited in its apis, and terribly slow. But if it can help, our process to setup pages consists in multiple calls to some apis:

    1. Checkout the page using https://tenant.sharepoint.com/sites/somesite/_api/sitepages/pages/GetByUrl('/sites/somesite/sitepages/home.aspx')/checkoutpage (POST). The result contains the CanvasContent1 field
    2. Parse the CanvasContent1 data, change its content according to your requirement
    3. Save the new CanvasContent1 value using https://tenant.sharepoint.com/sites/somesite/_api/sitepages/pages/GetByUrl('/sites/somesite/sitepages/home.aspx')/SavePage, With { "CanvasContent1": "<<encoded json of CanvasContent1>>"}. Updating the field value didn't worked for using PATCH on the listitem, but this API worked. This is probably the trickiest part.
    4. Publish the page using https://tenant.sharepoint.com/sites/somesite/_api/sitepages/pages/GetByUrl('/sites/somesite/sitepages/home.aspx')/publish