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:
There are two ways of obtaining the document libraries from a site collection:
- By using the Search API, you can get the libraries from the whole site collection (including the subsites).
- 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
- Part 1. Get libraries from the whole Site Collection
- Part 2: Get libraries from a single website
- Result
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:
Classic experience:
1. Choose the Insert tab in the Ribbon.
2. Click the blue Insert button.
Then, select the chosen Design Element from the ShortPoint Page Builder grid:
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:
Please switch to the Connect tab and choose REST API from the connection options:
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.
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.
2.2 Update the Advanced settings
In the same Connect tab, enable the 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;
2.3. Connect to the REST API
Click the Connect button:
After you see that the connection is successful, proceed to the next step to map the items:
The Path property in the Data Example results will be the link to the library:
Step 3: Map the Items
Switch to the 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:
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:
Then click Insert/Update to save your changes:
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:
Classic experience:
1. Choose the Insert tab in the Ribbon.
2. Click the blue Insert button.
Then, select the chosen Design Element from the ShortPoint Page Builder grid:
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:
Please switch to the Connect tab and choose REST API from the connection options:
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.
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.
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;
2.3. Connect to the REST API
Click the Connect button:
After you see that the connection is successful, proceed to the next step to map the items:
The LibraryUrl property in the Data Example results will be the link to the library:
Step 3: Map the Items
Switch to the 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:
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:
Then click Insert/Update to save your changes:
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:
Enjoy your REST-connecting!
Related articles:
- Connection Type: REST API
- Map Results Function for Your Custom REST APIs
- Modify URL Parameters Function for your REST API connection
- Modify the URL Path Function for Your REST API Connection
- How to Connect and Display a List of Subsites
- How to Show SharePoint Search Results in Any ShortPoint Design
- How to Display Content from a SharePoint List/Library Column in a Lightbox