Search code examples
office365sharepoint-onlinesharepoint-jsom

How do I get the list of all the document libraries present in the SharePoint site using JSOM?


One of the Projects I am working on, I have to get the list of all the document libraries in a site. Not sure how to achieve this since I am working with Office 365/SharePoint online.

I have a collaboration list which contains info for all the sites created under a site collection. For example the list has four column fields: Site Title, Site Description, URL Link to the Site and a Stage column (Choice column with choices: First, Intermediate and Final). So using this list and especially the URL Link Column field and when someone changes the stage value either from First or Intermediate to Final, I want to get the object of all the document libraries present in these sites. The user will manually change the stage value for each list item, either from First or Intermediate to Final and vice versa.

Thanks in Advance.


Solution

  • function getLibraries() {
    var context = SP.ClientContext.get_current();
    var lists = context.get_web().get_lists();
    context.load(lists,'Include(RootFolder, BaseTemplate)');
    context.executeQueryAsync(function () {
    var enumerator = lists.getEnumerator();
    while (enumerator.moveNext()) {
    var list = enumerator.get_current();
    var rootFolder = list.get_rootFolder();
    var url = rootFolder.get_serverRelativeUrl();
    if (list.get_baseTemplate() == '101')
    {
    console.log(url);
    console.log(list.get_baseTemplate() );
    }
    
    }
    },
    function (sender, args) {
    console.log('error');
    });   
    }
    

    This Code Will help you to get all present document libraries