我创建了一个网站集列,该列本身可以查找该网站集内的列表。我使用CSOM创建此列:
string contextUrl = "http://company.example.com/sites/mysite/subsite";
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
ClientContext clientContext = new ClientContext(contextUrl);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
Web rootWeb = clientContext.Site.RootWeb;
clientContext.Load(rootWeb);
// Add List containing target column
ListCreationInformation targetListInfo = new ListCreationInformation();
targetListInfo.Title = "TargetListTitle";
targetListInfo.TemplateType = (int)ListTemplateType.GenericList;
List targetList = web.Lists.Add(targetListInfo);
targetList.Update();
clientContext.Load(targetList);
clientContext.ExecuteQuery();
// Update Title
FieldCollection techListFields = targetList.Fields;
clientContext.Load(techListFields);
clientContext.ExecuteQuery();
// Create Site Lookupcolumns
var techListID = targetList.Id;
FieldCollection colSCFields = rootWeb.Fields;
clientContext.Load(colSCFields);
clientContext.ExecuteQuery();
var lookupSchema = "<Field Type='Lookup' DisplayName='Magic' Required='FALSE' List='" + techListID + "' ShowField='Title' StaticName='Magic' Name='Magic'/>";
colSCFields.AddFieldAsXml(lookupSchema, false, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();
(有关完整信息,我添加了所有行,但关键部分从"// Create Site Lookupcolumns"
)
正在创建网站列,但是当我在子网站内的列表上使用该列查找时(手动或通过程序)该查找字段的下拉列表不显示任何内容 。 (该行为看起来像是在目标列存在之前创建的查找列)
答案 0 :(得分:1)
您需要在字段xml定义中指定WebId属性值(将其设置为子网站ID),如下所示:
var lookupSchema = "<Field Type='Lookup' DisplayName='Magic' Required='FALSE' WebId='" + web.Id + "' List='" + techListID + "' ShowField='Title' StaticName='Magic' Name='Magic'/>";