0
votes

When I create document library in SharePoint via Client REST APIs I get different fields to identify library (like Title, EntityTypeName, Id). Sample response when I create library named "Школьные материалы 2.":

{
  "d": {
    "__metadata": {
      "id": "https://somesite.sharepoint.com/sites/Team_49de5296/_api/Web/Lists(guid'd94587ec-ff65-4b61-b0e8-2a00513494ee')"
       …
    },
    …
    "DocumentTemplateUrl": "/sites/Team_49de5296/2/Forms/template.dotx",
    …
    "EntityTypeName": "2",
    "Id": "d94587ec-ff65-4b61-b0e8-2a00513494ee",
    "ParentWebUrl": "/sites/Team_49de5296",
    "Title": "Школьные материалы 2."
  }
}

Though when I go to the SharePoint portal the URL for the library is https://somesite.sharepoint.com/sites/Team_49de5296/2/Forms/AllItems.aspx. Platform that I use supports only listUrls and for my library it is "/sites/Team_49de5296/2". How can I get it from the Create Document Library Response? Should I do separate query to get it? What property would it be?

1

1 Answers

0
votes

Yes, its not available in the Created list response.

But you can get it from a deferred property called RootFolder which will have a URL to make that request . It is not available by default for performance reasons.

So, you can modify from below sample code:

var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists";

$.ajax({
    url: fullUrl,
    type: "POST",
    data: JSON.stringify({
        '__metadata': { 'type': 'SP.List' },
        'BaseTemplate': 100,
        'Title': "Test JS List"
    }),
    headers: {
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function(data){
        console.log(data);

        // get the RootFolder endpoint 
        var listRootFolderEndpoint = data.d.RootFolder["__deferred"]["uri"];        

        $.getJSON(listRootFolderEndpoint, function( rootFolderListData ) {
            // Make a GET request and then get data 
            console.log(rootFolderListData);
            var listServerRelativeUrl = rootFolderListData.ServerRelativeUrl);
        }); 

    },
    error: function(data){
        console.log(data);
    }
});?