Sunday, 30 August 2015

How to programmatically get the Titles and Guids of all the lists in a Sharepoint site.

Hi all, Below code is used to get the details of all lists in the Site using CSOM.
I have logged the details in console. You can manipulate the data as per the requirement.

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext');
    retrieveAllListProperties();
});


function retrieveAllListProperties() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();
    clientContext.load(collList);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),       Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {

    var listInfo = '';
    var listEnumerator = collList.getEnumerator();
    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Guid: ' + oList.get_id().toString() + '\n';
    }
    console.log(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


Happy Coding :)