How to Retrieve all the pages from SharePoint Online sites and subsites using REST API


var oPagesCollection=[];//object having url for all pages under root site and sub site

getPage("") 





$.ajax({

url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",

method: "GET",

headers: {

    "Accept": "application/json; odata=verbose"

},

success: function(subsites) {

    $.each(subsites.d.results, function() {

        getSubSites(this.ServerRelativeUrl, this.Title);

        getPage(this.ServerRelativeUrl);

    });

    console.log(oPagesCollection);

},

error: function(subsites) {},

async: false

});



//function to get n level of subsites

function getSubSites(SubSiteUrl, SubSiteTitle) {

console.log(SubSiteUrl);

$.ajax({

    url: _spPageContextInfo.siteAbsoluteUrl + SubSiteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",

    method: "GET",

    headers: {

        "Accept": "application/json; odata=verbose"

    },

    success: function(subsites) {



        $.each(subsites.d.results, function(index) {

            getSubSites(this.ServerRelativeUrl, this.Title);

            getPage(this.ServerRelativeUrl);

        });

    },

    error: function(subsites) {},

    async: false

});

}



function getPage(siteUrl){

$.ajax({

url: _spPageContextInfo.webAbsoluteUrl+ siteUrl + "/_api/web/lists/GetByTitle('Pages')/items?$select=FileRef",

method: "GET",

headers: {

    "Accept": "application/json; odata=verbose"

},

success: function(data) {

    $.each(data.d.results,function(index,val){

        oPagesCollection.push(val["FileRef"]);

    });

},

error: function(rootsite) {},

async: false

});

}

Post a Comment

Previous Post Next Post

Recent Posts

Facebook