Is there any direct way to get the server relative URL of document library using its title using Rest API?

Keerthi Kumar picture Keerthi Kumar · Aug 8, 2017 · Viewed 9.5k times · Source

Presently I am using the following method to get the server relative URL

function getLibraryUrl(nameOrUrl)
    {
        var ctx = SP.ClientContext.get_current();
        var list = ctx.get_web().get_lists().getByTitle(nameOrUrl);
        ctx.load(list,'RootFolder');
        ctx.executeQueryAsync(
                            function(){
                            var url=list.get_rootFolder().get_serverRelativeUrl();
                            return url;
                            },
                            function(sender, args)
                                    {

     });    
    }

But, I don't want to use the asynchronous call. Can any one please guide me to get the server relative URL of document library using rest API.

Something like this /_api/Web/Lists/getByTitle('')/items?$select=File/ServerRelativeUrl&$expand=File

Answer

Rohit Waghela picture Rohit Waghela · Aug 8, 2017

Yes you can get ServerRelativeUrl of document library using REST API.

Refer below REST URL :

https://abcd.sharepoint.com/sites/RohitW/_api/Web/lists/getbytitle('DocLibTest1')/RootFolder

Output :

enter image description here