开发者问题收集

无法创建 LookUpField

2018-09-12
838

我正在创建一个新列表,并尝试在列表中添加一个 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":{}}
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 方法时发生的错误:

the parameter __metadata does not exist in method AddField.

似乎最新版本 ( 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 端点 ( ) 的有效负载中的 root 标记

根据历史记录 ,此错误是在较晚的一次提交中引入的,无论如何最好报告问题。

Vadim Gremyachev
2018-09-12