How can we help you today?

How to Show Top-Level Folders and Files from a Document Library Using REST API

This article shows how to display top-level files and folders from a Document Library using the REST API connection. Top-level means only folders and files in the root directory will be shown—files inside folders will remain hidden.


top level folders and files



NOTEThis guide will show only top-level files. If you want to see all the files, check out the articles below:
>How to Show Both Files and Folders of the Document Library in a ShortPoint Element
>How to Show a Mixed View of Files and Folders from a Document Library Using REST API

TABLE OF CONTENTS


Prerequisites


sample document library


Step-by-step Tutorial

Follow the steps below to get started:


Step 1: Edit the ShortPoint Web Part

Go to the SharePoint page you want to use and click Edit:


Edit


Close the Toolbox:


close icon


Click the Edit properties icon:


Edit properties icon


Step 2: Switch to Grid Mode


NOTEShortPoint Connect is currently only available in Grid Mode.

Click the Switch to Grid Mode icon:


Switch to Grid Mode icon


Step 3: Copy and Prepare the REST API URL


NOTEBefore proceeding, you must already have an existing Document Library with folders and files. To learn how to create a Document Library, check out Create a Document Library in SharePoint.

sample document library


Copy the REST API URL below:


<Your Site URL>/_api/Lists/GetByTitle('<Library Title>')/Items?$select=FileLeafRef,EncodedAbsUrl,Modified,Created,BaseName,FileDirRef,Editor/Title&$expand=Editor&$orderby=FSObjType%20desc,FileLeafRef%20asc


Substitute the items highlighted in red with your actual website URL and Document Library name. Here is an example of what the URL could look like:


https://yoursite.sharepoint.com/_api/Lists/GetByTitle('Guide')/Items?$select=FileLeafRef,EncodedAbsUrl,Modified,Created,BaseName,FileDirRef,Editor/Title&$expand=Editor&$orderby=FSObjType%20desc,FileLeafRef%20asc


Step 4: Edit the File List Design Element

NOTEBefore proceeding, make sure you already have the File List Design Element on your page:

file list design element


Hover over the File List and select the cogwheel icon:


Cogwheel icon


Step 5: Paste the REST API URL

Switch to the Connect tab:


Connect tab


Select REST API:


REST API


Paste the REST API URL you prepared in Step 3 in the field provided:


paste


Step 6: Connect

Scroll down and toggle on Enable Advanced Settings:


Enable advanced settings


Copy the code below:

function getFolderFromUrl(url) {
 const parts = url.split('/');
 return parts.at(-1);
}
var resultsWithType = data.d.results.map(function(item) {   
  if(item.FileLeafRef != null) {     
    var lastDotPosition = item.FileLeafRef.lastIndexOf('.');
    item['Type'] = item.FileLeafRef.substring(lastDotPosition + 1, item.FileLeafRef.length);     
  }
  return item;
});
return resultsWithType.filter(item => (getFolderFromUrl(item.FileDirRef) === "<Library Title>"))

Paste the code in the Map Results field and replace <Library Title> with the name of your Document Library:


paste


Click Connect:


Connect


Step 7: Map Items

Switch to the Items tab:


Items tab


Delete all default content:


delete default content


Use the link icon to map what will be displayed as the Title, Subtitle, and Description. In our example, we will showcase the file name as the title, the date created as the subtitle, and the owner of the file as the description:


map items


NOTEYou can customize what information appears in the Title, Subtitle, and Description fields. Feel free to use our example or choose your own preferred display options.

Set #Type as the File Type and #EncodedAbsUrl as the Link:


file type and link


Step 8: Preview and Save

Select Preview to see how the list will look:


Preview


Once satisfied, click Update:


Update


Click the eye icon to see your page in real-time:


eye icon


Click Save to apply all your changes:


Save


That’s it! Only top-level files and folders are shown inside your connected File List.


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 33 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