Question

I need to get the folder name within a document library, within the ListCommand spfx extension. Is there a way to do that with REST ? can I use @pnp/sp ?

I have imported the project with :

import { sp } from "@pnp/sp";
Was it helpful?

Solution

You can use below PnPjs code to get all folders from a library and print name and url:

import { sp } from "@pnp/sp";

sp.web.lists.getByTitle("Documents").items.filter('FSObjType eq 1').select('FileLeafRef', 'FileRef').get()
.then(folders => {
    folders.forEach(f => {
        console.log('Folder name: ' + f.FileLeafRef);
        console.log('Folder url: ' + f.FileRef);

    })
}) 

FSObjType (display name Item Type) is a built-in field, values are based on this enumeration

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top