Q: Create list item inside folder with SPFx
-
07-02-2021 - |
Вопрос
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'.
Решение
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);
Другие советы
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.
Не связан с sharepoint.stackexchange