0

I was trying to creating an SPFX solution using React where on submit of form I am saving multiple items inside a folder in SharePoint list.

I understood how to create folder inside SharePoint list using PNP but not able to create items inside the created folder.

Below is the code for creating folder in SharePoint list.

let list = sp.web.lists.getByTitle('TestList');
return list.items
  .add({
    Title: 'Dummy',
    FileSystemObjectType: 1,
    ContentTypeId: '0x0120'
  })
  .then(({ item }) => {
    return item.update({
      Title: 'New Folder',
      FileLeafRef: 'New Folder' 
    });
  }).then((f) => {console.log("Folder created")})

It would be really helpful if anyone has any idea how to create, update get item inside folder in SharePoint list.

5
  • 1
    Check answer given at: sharepoint.stackexchange.com/questions/264136/… Nov 20 '20 at 12:36
  • Could you please also let me know how to get items inside folder in sharepoint list using pnp js by using folder name?
    – Sz4
    Nov 23 '20 at 8:03
  • 1
    Is above code working for you to create folder? To get items from folder, check this. Nov 23 '20 at 8:22
  • Yes adding and get Item is working. Thank you very much. One doubt is like how to check if folder alredy exists and updating item inside any idea?
    – Sz4
    Nov 23 '20 at 10:35
  • Great. I have summarized all the comments and added it as an answer below. Please Upvote(^) and accept as an Answer as it helped you. Regarding your question to check if folder exists and updating item, I will suggest you to create a new question and explain your requirements in detail. Nov 23 '20 at 11:25
1

You can use addValidateUpdateItemUsingPath to create list items inside folder.

Sample code:

const listPath = `${webRelativePath}/Lists/ListUri`;

sp.site.rootWeb.lists.getById(ListId).addValidateUpdateItemUsingPath([
    { FieldName: 'Title', FieldValue: 'TestItem' }
], `${listPath}/FolderPath`).then(console.log);

To get items inside folder, you can try filtering on FileDirRef property like given in below reference.

Sample code:

import { sp } from '@pnp/sp';
const listUri = '/sites/site/Lists/list';
sp.web.getList(listUri).items
    .filter(`FileDirRef eq '${listUri}/SubFolder01'`)
    .get().then(console.log);

Reference: Get All Items from a List subfolder

3
  • could you please let me knowin case you find something like updating items already added in folder sharepoint list using PNP JS?
    – Sz4
    Nov 24 '20 at 14:28
  • 1
    While updating SharePoint list item, you will need its item ID. If you have item ID with you then try using the Update method if it works. Nov 24 '20 at 14:57
  • FileDirRef is an issue incase there are more than 5000 items. Is there any other way around
    – Sz4
    Dec 11 '20 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.