SPFx:将新项目添加到现有列表文件夹(使用pnp)

时间:2020-02-19 15:50:09

标签: typescript spfx

有人知道如何在Sharepoint Framework中将新项目添加到列表文件夹吗?

通过这种方式,我可以将项目添加到列表MyList

let list = pnp.sp.web.lists.getByTitle("MyList");    

list.items.add({
      Title: "itemName"
      }).then(r => { 

      console.log(r);
})

但是我需要在文件夹MyList\MyFolder中创建它。

我尝试以这种方式检索文件夹:

var folder= sp.web.lists.getByTitle("MyList").rootFolder.folders.getByName("MyFolder");

但我找不到在其中添加项目的方法。

2 个答案:

答案 0 :(得分:1)

sp-pnp-js库似乎没有实现此操作。

您应该能够使用如下所示的手动获取请求来做到这一点:

const listTitle = "List Title";
const folderTitle = "Folder Title";
const webUrl = _spPageContextInfo.webAbsoluteUrl;
const url = webUrl + "/_vti_bin/ListData.svc/" + listTitle.replace(/\s/g, "");
fetch(url, {
    method: 'POST',
    headers: {
        "Accept": "application/json",
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        Title: "Item Name",
        Path: webUrl + "/Lists/" + listTitle + "/" + folderTitle
    }),
}).then(console.log);

答案 1 :(得分:0)

就像杰克逊所写的那样,似乎无法通过sp-pnp-js库将项目添加到列表文件夹中。

因此,通过一些搜索,我发现可以为此目的使用addValidateUpdateItemUsingPath

const _spPageContextInfo=this.context.pageContext.legacyPageContext;
const webUrl = _spPageContextInfo.webAbsoluteUrl;

const listPath=webUrl+"/Lists/MyListInternalName";
const folderName="MyFolderName";

sp.site.rootWeb.lists.getByTitle("MyList").addValidateUpdateItemUsingPath([
    { FieldName: 'Column1', FieldValue: 'Value1'}, 
    { FieldName: 'Column2', FieldValue: 'Value2'}]    
  ,`${listPath}/${folderName}`).then(console.log);

然后,我花了一些额外的时间来了解如何在使用addValidateUpdateItemUsingPath时设置User字段的FieldValue(在这种情况下,使用用户ID-照常-来检索用户无法正常工作) )。
但是我发现了这种方式:

{FieldName: 'ColumnUser', 
FieldValue: JSON.stringify([{"Key": "i:0#.f|membership|"+"MyUserEmail"}])}