In this blog, you will learn how to retrieve the webpart properties from a SharePoint page using REST API.
The page can be accessed using the page relative URL. The following REST API URL can be used to access the webpart properties.
- /_api/web/GetFileByServerRelativeUrl('/Pages/ToolPage4.aspx')/GetLimitedWebPartManager(1)/WebParts?$expand=Webpart
While using the GetLimitedWebPartManager method, provide the personalization scope to be used in retrieving properties. The scope can be Shared scope or user specific scope. The scope values are 1 and 0 respectively.
$expand is used to expand the properties and it is generally used in REST API calls. Failing to expand the properties, the response will not fetch the webpart property values.
Ajax is used to call the SharePoint API.
The following code snippet shows the ajax call to retrieve the webpart properties from SharePoint page.
- $(document).ready(function(){
- var restURL = "/_api/web/GetFileByServerRelativeUrl('/Pages/ToolPage4.aspx')/GetLimitedWebPartManager(1)/WebParts?$expand=Webpart"
- $.ajax({
- url: restURL,
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- async: false,
- cache: false,
- success: function (data) {
- if(data != null && data.d != null){
- var properties = data.d.results[0];
- var title = data.d.results[0].WebPart.Title;
- }
- },
- error: function (data) {
- }
- });
-
- });
The response will be objects (set of webparts) present on the page. The following image shows all the webparts from a page and webpart properties along with Id. If you expand the properties here, you will get more details about the webpart.
Note: The image shows the response of the above Ajax call on Chrome's debugger.