我正在创建一个新列表并尝试在列表中添加一个 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":{}}