1

我正在创建一个新列表并尝试在列表中添加一个 LookUp 字段。

我检索要查找的列表:

let list = sp.web.lists.getByTitle("Trucks");
    list.get().then(list => {
        this._ensureMyList("MySPListTest", list)
    }); 

然后我创建新列表和新字段:

private _ensureMyList(listName: string, truckList): void {
    sp.web.lists.ensure(listName)
        .then((ler: ListEnsureResult) => {
            if (ler.created) {
                console.log("list was created");

                ler.list.fields.add("LookupTest", "SP.FieldLookup", { 
                    Group: "~Example",
                    FieldTypeKind: 7,
                    Filterable: true,
                    Hidden: false,
                    EnforceUniqueValues: true,
                })
                    .then((result) => {
                        console.log("result: ", result);
                    });
            }
        });
}

我得到错误:

Uncaught (in promise) Error: Error making HttpClient request in queryable: [500]  ::> {"responseBody":{"odata.error":{"code":"-2146232832, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"Please use addfield to add a lookup field instead."}}},"responseHeaders":{}}

我从以下位置获得了字段名称和 TypeKind:

https://msdn.microsoft.com/en-us/library/office/dn600182.aspx#bk_FieldLookup

我也尝试使用该addLookup方法:

ler.list.fields.addLookup("LookupTest", truckList.Id, "Truck")
    .then((result) => {
        console.log("result: ", result);
    });

有错误:

Uncaught (in promise) Error: Error making HttpClient request in queryable: [400]  ::> {"responseBody":{"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The parameter __metadata does not exist in method AddField."}}},"responseHeaders":{}}
4

1 回答 1

1

您可以考虑以下选项来添加查找字段:

1)通过Fields.addLookup方法

let list = await sp.web.lists.getByTitle(listTitle);  //get list resource
let lookupList = await sp.web.lists.getByTitle(lookupListTitle).select("Id").get();  //select Lookup List Id 
let field = await list.fields.addLookup(lookupFieldName, lookupList.Id ,"Title"); // add lookup field to list

2)通过Fields.createFieldAsXml方法:

let list = await sp.web.lists.getByTitle(listTitle); //get target list resource
let lookupList = await sp.web.lists.getByTitle("Categories").select("Id").get(); //determine lookup list id
let fieldXml = `<Field Name="Category" Type="Lookup" DisplayName="Category" List="{${lookupList.Id}}" ShowField="Title"/>`;  //construct lookup field schema
let field = await list.fields.createFieldAsXml(fieldXml);  //add field to list

参考

PnPjs 添加字段

更新

Fields.addLookup关于调用方法时发生的错误:

方法 AddField 中不存在参数 __metadata。

方法v1.2.1的最新版本( )中 似乎存在错误。关键是它会生成无效的有效载荷,如下所示:Fields.addLookup

{
   "__metadata":{
     "type":"SP.FieldCreationInformation"
   },
   "parameters": { 
          "FieldTypeKind":7,
          "LookupFieldName":"Title",
          "LookupListId":"{list-id}",
          "Title":"Cat"
   }
} 

parameters应该是端点有效载荷中的标记(http://<site url>/_api/web/lists(guid'<list id>')/fields('<field id>')/addfield

根据历史,这个错误是在后来的一次提交中引入的,无论如何最好报告一个问题。

于 2018-09-12T23:08:51.790 回答