4

I am working with O365 SharePoint site where I need to get the name and URL for the document libraries in a site collection. I have made a rest call for it as:

https://site/_api/web/lists and select BaseTemplate = 101.

I have managed to get the name of the document library from below code:

$.ajax({
        url: siteUrl  + "/_api/web/lists",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {                                  
            var tempArray = data.d.results;
             $.each(data.d.results, function(index, item){  
                 if(item.BaseTemplate == "101")
                 {
                    var tempArray = new Array(2);                      
                    tempArray[0]= item.Title;                      
                 }
            });
        },
        error: function (data) {
           alert(JSON.stringify(error));                    
        }

Now, I need to get the URL of the each document library. When I access the value of

        item.__metadata.uri

it gives me a value which is a url that is not accessible otherwise. Please get me out of this problem.

1 Answer 1

3

In order to return list url you could utilize List.RootFolder.ServerRelativeUrl property.

Some recommendations

From performance perspective prefer server side filtering over client side one, the example below demonstrates how to query lists filtered by BaseTemplate:

/_api/Web/Lists?filter=BaseTemplate eq '101' 

Example

$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl  + "/_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){  
                 var listUrl = list.RootFolder.ServerRelativeUrl;
                 console.log(listUrl);  
             });
        },
        error: function(data) {
           console.log(JSON.stringify(data));                    
        }
      });
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.