Pnpjs: Retrieving folders of a list with pnpjs

Created on 21 May 2019  路  6Comments  路  Source: pnp/pnpjs

I want to retrieve folders inside a list This is the code I am trying to use:

sp.web.lists.getByTitle(listName).rootFolder.folders

But this doesn't return a list of folders inside that list, instead it returns an object with props like: _url, _parentUrl.

How do I retrieve an array of folders inside a list/library with PnPJS?

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue
code answered question

Most helpful comment

Hi there,

@Danielaroc you didn't send an actual request, just created a chainable object. To retrieve actual data .get() method should be used. It is a Promise so async/await or then/catch go next. Please check documentation samples, all queriable objects shares the same approach.

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

const listName = 'CustomList';

const list = sp.web.lists.getByTitle(listName);

// Get list's root folders and their items' props
list.rootFolder.folders
    .filter('ListItemAllFields/Id ne null')
    .expand('ListItemAllFields')
    .get()
    .then((folders) => console.log(folders))
    .catch(console.error);

// Get folders' items recursively
list.items.filter(`FSObjType eq 1`).get()
    .then((folders) => console.log(folders))
    .catch(console.error);

All 6 comments

Are you trying to get all folders at the top level? All folders recursively?

@sympmarc Just the folders at the top level. But would be good to know how to do so recursively for sub folders as well!

Hi there,

@Danielaroc you didn't send an actual request, just created a chainable object. To retrieve actual data .get() method should be used. It is a Promise so async/await or then/catch go next. Please check documentation samples, all queriable objects shares the same approach.

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

const listName = 'CustomList';

const list = sp.web.lists.getByTitle(listName);

// Get list's root folders and their items' props
list.rootFolder.folders
    .filter('ListItemAllFields/Id ne null')
    .expand('ListItemAllFields')
    .get()
    .then((folders) => console.log(folders))
    .catch(console.error);

// Get folders' items recursively
list.items.filter(`FSObjType eq 1`).get()
    .then((folders) => console.log(folders))
    .catch(console.error);

@koltyakov Thank you so much!

Can you please tell me what's meant by these 2 lines:

 .filter('ListItemAllFields/Id ne null')
    .expand('ListItemAllFields')

I didn't understand why you needed to filter and expand by ListItemAllFields/Id

If remove these, all the folders not only that are actual items will be retrieved. In most of the cases, service folders (e.g. Attachments) are not what you want to get querying a list for folders.
ListItemAllFields also is handy for getting folder item's properties as SP.Folder object not likely contains what you expect to see as "list folder metadata".

Thank you @koltyakov

Was this page helpful?
0 / 5 - 0 ratings