How can we help you today?

How to Retrieve All Document Libraries from a Site Collection Using ShortPoint Connect

If you want to retrieve all document libraries from a SharePoint site collection, this article is created for you. Using ShortPoint REST API connection, you will be able to display them on your SharePoint page. It will look similar to this:



Libraries of the Site Collection - exampleThere are two ways of obtaining the document libraries from a site collection:

  1. By using the Search API, you can get the libraries from the whole site collection (including the subsites).
  2. By using the REST API, you can get the libraries from a single specific website.

In this tutorial, we will show you both options. Let's start!


TABLE OF CONTENTS


This solution is for you if

  • You use Office 365 or SharePoint 2019 environment.
  • You use Modern or Classic SharePoint experience.
  • You have ShortPoint installed on your SharePoint sites(s).
  • You are a ShortPoint user with an active license.

Part 1. Get libraries from the whole Site Collection


The following solution uses the Search API. Please follow the steps to configure the connection.


Step 1: Add a ShortPoint Design Element to the page


Add any ShortPoint Design Element to the page. Design Elements can be easily added to your pages using ShortPoint Page Builder.


  • Modern experience:

In the edit mode of the page, click the blue Insert button to open the ShortPoint Page Builder:


Insert (modern experience)


  • Classic experience:

1. Choose the Insert tab in the Ribbon.

2. Click the blue Insert button.


Insert (classic experience)


Then, select the chosen Design Element from the ShortPoint Page Builder grid: 


Files List Design Element


Idea: For the purpose of this article, we will add the File Lists Design Element, however you can use any other Design Element you like.

Step 2: Configure the REST API settings


2.1 Set up the REST API connection URL


Once you've added a ShortPoint Design Element, the Settings tab will open:

Settings tab


Please switch to the Connect tab and choose REST API from the connection options:

REST API option in the Connect tab


In the REST API URL, paste the following URL (items highlighted with red and yellow require editing):


https://<siteUrl>/_api/search/query?querytext='STS_List_DocumentLibrary AND SiteId:{<siteId>}'&trimduplicates=false


  • <siteURL> - should be replaced with the site URL libraries of which you would like to display;
  • <siteId> - should be replaced with the site ID libraries of which you would like to display. The siteId value is a set of numbers and letters, and it looks similar to this: 1d2b3d31-14f6-458e-be6e-6dd267460f44.


Note: You can get the siteId value by opening the Developer Tools on your site (F12 or Ctrl+Shift+I (for Windows) / Cmd+Shift+I (for macOS)) and writing the following command in the console: _spPageContextInfo.siteId.

_spPageContextInfo.siteId command in Console


For example, our REST API URL looks like this:

https://mycompany.sharepoint.com/_api/search/query?querytext='STS_List_DocumentLibrary AND SiteId:{1d2f3d31-14f6-658e-be6e-6dd127460f44}'&trimduplicates=false

Where mycompany.sharepoint.com is our site collection URL, and 1d2f3d31-14f6-658e-be6e-6dd127460f44 is the siteId.


REST API URL


2.2 Update the Advanced settings


In the same Connect tab, enable the Enable Advanced Settings:

Enable Advanced Settings


Scroll down to the Map results section and paste the code below to its text field:


var returnData = [];
var $ = shortpoint.$;
var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
$.each(results, function () {
var row = {};
$.each(this.Cells.results, function () {
row[this.Key] = this.Value;
});
returnData.push(row);
});
return returnData;


Map results function


2.3. Connect to the REST API


Click the Connect button:


Connect button


After you see that the connection is successful, proceed to the next step to map the items:

Successfully connected


The Path property in the Data Example results will be the link to the library:

Path property in Data Example


Step 3: Map the Items


Switch to the Items tab:


Items tab


Press the chain icon on the right of the Design Element field (for example, Author) and select which column of your SharePoint list/library correspond to it:

Mapping options drop-down


Note: To know more about items mapping, please check our ShortPoint Connect: Basic Tutorial.


You can use Path for the Link field.

After you are done with the items mapping, you may click the Preview button to check the results:


Preview button


Then click Insert/Update to save your changes:

Insert/Update


Additional: Connection Tips and Limitations


SharePoint Search Dependency


The described connection approach uses the SharePoint Search API, meaning that the connection will depend on the SharePoint Search. This dependency affects the results: they will not be displayed in real time. For example, if you just created a new document library on your site, the Search API will not display it instantaneously, but just after it will get indexed by the Search Crawl.


Sorting


Sorting is only possible by Sortable Managed Properties. Please note that the default Title and Path properties are not sortable. In order to make them sortable, you will have to map them on a RefinableStringXX (default) managed properties from the Search Admin of your Tenant. Follow this article for more guidance: Make the title field sortable in search web parts.

Here is an example of the API which sorts the results based on a refinable/sortable managed property: https://<siteUrl>/_api/search/query?querytext='STS_List_DocumentLibrary AND SiteId:{<siteId>}'&trimduplicates=false&sortList='RefinableString01:ascending'


Exclude some items from the results list


If you have to exclude some items from the results list, you will need to use refiners with negation filters. 


For example, if you would like to exclude Site Assets and Shared Documents libraries, your REST API URL will look similar to this:

https://<siteUrl>/_api/search/query?querytext='STS_List_DocumentLibrary AND SiteId:{<siteId>}'&trimduplicates=false&refinementfilters='and(RefinableString01:not("SiteAssets"), RefinableString01:not("Shared Documents"))'

*Assuming that RefinableString01 is mapped to the Title property.


Bring Document Libraries from the whole Tenant


Remove the "SiteID:" part from the REST API URL to get document libraries from your whole tenant.

The URL should look like this:

https://<siteUrl>/_api/search/query?querytext='STS_List_DocumentLibrary'&trimduplicates=false


Bring Document Libraries from "Team Sites" in your Tenant


Add &refinementfilter='SiteTemplate:Group' to the end of the URL: 

https://<siteUrl>/_api/search/query?querytext='contentclass=STS_List_DocumentLibrary'&trimduplicates=false&refinementfilters='SiteTemplate:Group'


Part 2: Get libraries from a single website


The following solution uses the REST API. Please follow the steps to configure the connection.


Step 1: Add a ShortPoint Design Element to the page


Add any ShortPoint Design Element to the page. Design Elements can be easily added to your pages using ShortPoint Page Builder.


  • Modern experience:

In the edit mode of the page, click the blue Insert button to open the ShortPoint Page Builder:


Insert (modern experience)


  • Classic experience:

1. Choose the Insert tab in the Ribbon.

2. Click the blue Insert button.


Insert (classic experience)


Then, select the chosen Design Element from the ShortPoint Page Builder grid: 


Files List Design Element


Idea: For the purpose of this article, we will add the File Lists Design Element, however you can use any other Design Element you like.

Step 2: Configure the REST API settings


2.1 Set up the REST API connection URL


Once you've added a ShortPoint Design Element, the Settings tab will open:

Settings tab


Please switch to the Connect tab and choose REST API from the connection options:

REST API option in the Connect tab


In the REST API URL, paste the following URL:


https://<siteUrl>/_api/Web/Lists?$filter=BaseTemplate eq 101

Replace the <siteUrl> with the URL of your SharePoint site or subsite.


For example, our REST API URL looks like this:

https://mycompany.sharepoint.com/_api/Web/Lists?$filter=BaseTemplate eq 101

Where mycompany.sharepoint.com is our site URL.


REST API URL


This request will not return the URL of the libraries directly in the results, so we'll have to compound it via a mapping function, as described in the next step.


2.2 Update the Advanced settings


In the same Connect tab, enable the Enable Advanced Settings.

Enable Advanced Settings


Scroll down to the Map results section and paste the code below to its text field:


var results = data.d.results;
var tenantUri = window.location.protocol + "//" + window.location.host;
for(var i=0;i<results.length;i++){
results[i]["LibraryUrl"] = tenantUri + results[i]["ParentWebUrl"] + "/" + results[i]["EntityTypeName"];
}
return results;


Map results function

2.3. Connect to the REST API


Click the Connect button:


Connect button


After you see that the connection is successful, proceed to the next step to map the items:

Successfully connected


The LibraryUrl  property in the Data Example results will be the link to the library:

LibraryUrl property in Data Example


Step 3: Map the Items


Switch to the Items tab:


Items tab


Press the chain icon on the right of the Design Element field (for example, Title) and select which column of your SharePoint list/library correspond to it:

Mapping options drop-down


You can use LibraryUrl for the Link field.


Note: To know more about items mapping, please check our ShortPoint Connect: Basic Tutorial.


After you are done with the items mapping, you may click the Preview button to check the results:


Preview button


Then click Insert/Update to save your changes:

Insert/Update


Additional: Connection Tips


Sorting


Using this connection, you can sort the results by simply adding the $orderby parameter to the URL.

For example, if you would like to sort the document libraries by Title in descending order, your REST API URL will look similar to this:

https://<siteUrl>/_api/Web/Lists?$filter=BaseTemplate eq 101&$orderby=Title desc


Exclude some items from the results list


If you have to exclude some items from the results list, you will need to use ne (not equal) operator. 

For example, if you would like to exclude Site Assets and Shared Documents libraries, your REST API URL will look similar to this:

https://<siteUrl>/_api/Web/Lists?$filter=BaseTemplate eq 101 and Title ne 'Site Assets' and Title ne 'Style Library'


Result


Here is how your end result may look like on your page:


Page example of the applied solution


Enjoy your REST-connecting!



Related articles:

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

World's best intranet sites are designed using ShortPoint

Get started today! Learn more
See all 31 topics

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more