3

Is it possible to get list of site collections using JavaScript Object Model in SharePoint Online?

I would like to display a list of Site Collections in left navigation.

something like below.

>SC1

  >Document Lib

    >Folder1

>SC2

  >Document Lib

    >Folder2

Would like to know if this is a possible solution or not?

Update: Answer by @Mancy Desaee gets the structure within a site collection and I guess following that approcah I can solve my problem.

2

Yes, it is possible to get it. You can use REST calls to get the required information. The following code shall help you: I have created respective arraylists to store the subsite, document library and folder information. Once the information is collected in the arraylists we can display it as per out requirement.

Get subsites:

$.ajax({
    url: siteUrl + "/_api/web/webs/?$select=title,ServerRelativeUrl,Created,effectivebasepermissions&$filter=(effectivebasepermissions/high%20gt%2032)&$orderby=Created desc",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(data) {
        var tempArray = data.d.results;
        $.each(data.d.results, function(index, item) {
            var tempArray = new Array(2);
            tempArray[0] = item.Title;
            tempArray[1] = item.ServerRelativeUrl;
            SubsiteDetails.push(tempArray);
        });
    },
    error: function(data) {
        //alert(JSON.stringify(data));
    }

Get Document Libraries in subsites:

$.ajax({
    url: siteUrl + "/_api/web/lists?filter=BaseTemplate eq '101'&$expand=RootFolder",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(data) {
        var lists = data.d.results;
        $.each(lists, function(i, list) {
            if (list.BaseTemplate == "101") {
                var listUrl = list.RootFolder.ServerRelativeUrl;
                var tempArray = new Array(2);
                tempArray[0] = list.Title;
                tempArray[1] = siteUrl + listUrl;
                docLibList.push(tempArray);
            }
        });
    },
    error: function(data) {
        //alert(JSON.stringify(error));
    }

Get folders in document libraries:

var url = siteUrl + "/_api/web/lists/getbytitle('" + documentLibrary + "')/rootFolder/Folders?$expand=ListItemAllFields";
$.getJSON(url, function(data) {
    //print folders
    $(data.value).each(function(i, folder) {
        if(folder.Name != "")
        {               
            if (folder.Name != "Forms" && folder.Name != "Attachments" && folder.Name != "Item") {                
                var tempArray = new Array();
                tempArray = [];
                tempArray[0] = folder.Name;
                tempArray[1] = siteUrl + folder.ServerRelativeUrl; 
                tempArray[2] = folder.ServerRelativeUrl;

                if(tempArray != []){                       
                    folderDetails.push(tempArray);
                }
            }
        }  
    });

Here the SharePoint system folders have been skipped.

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.