Monday, May 11, 2015

Get all the web parts id of particular page using REST API.

The below code used to get the all the web parts id for the particular page.

function GetWebParts() {
    var executor = new SP.RequestExecutor(appweburl);
    var urltest = appweburl + "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('/sites/test017/Pages/default.aspx')/getlimitedwebpartmanager(scope=0)/WebParts?@target='" + hostweburl + "'";
    $.ajax({
        url: urltest,
        method: "GET",
        async: false,
        headers: {
            Accept: "application/json; odata=verbose"
        },
        success: function (data) {
            var d = "";
            if (data.d.results.length > 0) {
                for (var i = 0; i < data.d.results.length; i++) {
                    d += "<div>" + data.d.results[i].Id + "</div>"
                }
                $("#appWebLists").html(d);
            }
        },
        error: function (n) {
            SP.UI.Notify.addNotification("<span>Request failed. " + n.status + "\n" + n.statusText + "<\/span>");
        }
    });
}

1 comment:

  1. You can also get this information using the Office Dev PnP Engine to achieve the same. It can be found here: https://github.com/OfficeDev/PnP/blob/master/Solutions/PowerShell.Commands/Documentation/GetSPOWebPart.md

    ReplyDelete