3

I have few content editor web parts in my page

I need to get the GUID of all the web parts that are there in the page

I am able to get the list of all webparts in the page by navigating to

http://pageurl?content=1

I couldnt find the GUID's of the webparts, how can i get them using REST/CSOM or any other client side method?

3

Using JSOM

 var clientContext = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
    var oFile = clientContext.get_web().getFileByServerRelativeUrl(_spPageContextInfo.serverRequestPath);
    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var collWebPart = limitedWebPartManager.get_webParts();

    clientContext.load(collWebPart);
    clientContext.executeQueryAsync(Function.createDelegate(this, function () {
        var webPartDef = null;
        for (var x = 0; x < collWebPart.get_count() && !webPartDef; x++) {
            var temp = collWebPart.get_item(x);
            if (temp.get_id().toString() === wpId) {
                webPartDef = temp;
            }
        }
        if (!webPartDef) {
            dfd.reject("Web Part: " + wpId + " not found on page: " + _spPageContextInfo.webServerRelativeUrl);
            return;
        }

        var webPartProperties = webPartDef.get_webPart().get_properties();
        clientContext.load(webPartProperties);
        clientContext.executeQueryAsync(Function.createDelegate(this, function () {
            dfd.resolve(webPartProperties, webPartDef, clientContext);
        }), Function.createDelegate(this, function () { dfd.reject("Failed to load web part properties"); }));
    }), Function.createDelegate(this, function () { dfd.reject("Failed to load web part collection"); }));

    return dfd.promise();
}

http://dluat.com/reasonable-way-to-get-custom-webpart-properties-via-jsom/

Using REST

_api/Web/GetFileByServerRelativeUrl('/sites/test/Home.aspx')/GetLimitedWebPartManager(scope=1)/WebParts?$expand=WebPart&$filter=WebPart/Title+eq+'Test'

You can get webpartcollection using the webpartmanager.WebPartManager control acts as the hub or control center of a Web Parts application.

There must be one and only one WebPartManager control instance on every page that uses Web Parts controls.

  • 1
    Thanks Amit Anyway that can be done via clientside code? – Vignesh Subramanian Jun 16 '15 at 5:28
  • 1
    See my edited answer – Amit Kotha Jun 16 '15 at 5:40
  • Thanks Amit, upvoted Will try this out :) – Vignesh Subramanian Jun 16 '15 at 5:53

Your Answer

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

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