我有一个名为“Books”的列表,其中包含“Name”,“AuthorName”,“ISBN”列,类型为文本。现在我有另一个名为“BillTokenStore”的列表,我想在“BillTokenStore”中添加查找列'AuthorName'。以下是我的所作所为。
using (ClientContext context = new ClientContext(webFullUrl: siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName, GetPassWord());
Web web = context.Web;
ListCollection listCollection = web.Lists;
List list = listCollection.GetByTitle("BillTokenStore");
string schemaLookupField = @"<Field Type='Lookup' Name='InStock' StaticName='InStock' DisplayName='InStock' List = 'Books' ShowField = 'Title' /> ";
Field lookupField = list.Fields.AddFieldAsXml(schemaLookupField, true, AddFieldOptions.DefaultValue);
context.ExecuteQuery();
}
当我运行此代码时,我收到错误“值不在预期范围sharepoint 2013内”。这有什么不对?在此先感谢。
注意:我能够通过UI实现相同的功能。我还可以添加其他类型的字段,如choice,boolean和所有代码。
答案 0 :(得分:1)
您需要显式加载列表和该列表的字段。
此外,我们需要传递查阅列列表的GUID。
请尝试以下修改后的代码:
using (ClientContext context = new ClientContext(webFullUrl: siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName, GetPassWord());
Web web = context.Web;
List booksList = context.Web.Lists.GetByTitle("Books");
List list = context.Web.Lists.GetByTitle("BillTokenStore");
context.Load(list, l => l.Fields);
context.Load(booksList, b => b.Id);
context.ExecuteQuery();
string schemaLookupField = @"<Field Type='Lookup' Name='InStock' StaticName='InStock' DisplayName='InStock' List='"+ booksList.Id +"' ShowField = 'Title' />";
Field lookupField = list.Fields.AddFieldAsXml(schemaLookupField, true, AddFieldOptions.DefaultValue);
lookupField.Update();
context.Load(lookupField);
context.ExecuteQuery();
}