1

I am trying to create an item within a folder in a list.

Is there an approach with pnpjs that looks like the following?

TypeScript

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

const shpList = sp.site.rootWeb.lists.getById(ListId);

let newItemWithPath = {
    ...otherItemProps,
    Path: `/sites/site/lists/list/${folderName}/`
};

shpList.items.add( newItemWithPath );

The previous code gives the error:

The property 'Path' does not exist in the type 'SP.Data.ListNameListItem'.

0

Yes you can via the addValidateUpdateItemUsingPath method of which accepts a path to your subfolder as a parameter.

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

sp.site.rootWeb.lists.getById(ListId).addValidateUpdateItemUsingPath([
    { FieldName: 'Title', FieldValue: 'TestItem' }
], `${listPath}/FolderPath`).then(console.log);
  • Thanks for the response! But I think I can't guess the FolderPath. Given that FolderPath is different to the folder name. Is it the case? Or am I writing it wrong? – the simple Jun 4 at 9:50
  • If your folderName is dynamic then you can try using something like this: ${listPath}/${folderName}. – Ganesh Sanap Jun 4 at 13:32
  • So far it looks like this is the correct answer for my question but I have done something wrong. Should I mark this as the answer and make other thread for my problem? (${listPath}/${folderName} is in fact also the path to the folder, I can access by typing in the searchbar, but the scripts gives an error (There is not any folder with that URL)) – the simple Jun 4 at 14:11
  • Hi The Simple, yes please mark as answer and raise another question. I will keep an eye out for it and assist if i can, thanks – James Randal Jun 4 at 23:02
  • The key was to use an absolute URL to the folder (www. ...) . Any other options gave me errors. – the simple Jun 5 at 10:13
0

It is possible to create a list item within a folder using AddValidateUpdateItemUsingPath.

You need to use the request body similar to below code:

{
    "listItemCreateInfo": {
        "FolderPath":  { "DecodedUrl": "https://contoso.sharepoint.com/lists/Test/Folder/SubFolder" },
        "UnderlyingObjectType": 0
    },
    "formValues": [
        {
            "FieldName": "Title",
            "FieldValue": "Item"
        }
    ],
    "bNewDocumentUpdate": false
}

Reference: Create List Item in a Folder.

  • Making a call with that body returns the following error: "Error processing request stream. The property name 'listItemCreateInfo' specified for type 'Microsoft.SharePoint.DataService.ListNameItem' is not valid." Any idea of the possible causes? Thanks for the help in both my question, by the way. – the simple Jun 4 at 11:51

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.