查找网站集上的列不包含内容

Lookup to column on site collection does not contain contents

我创建了一个网站集列,该列本身可以查找该网站集中的列表。我使用 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"

正在创建网站栏,但是当我在子网站内的列表中使用查找该栏时(手动或通过程序)该查找字段的下拉列表不显示任何内容。 (该行为看起来像是在目标列存在之前创建的查找列)

您需要在字段 xml 定义中指定 WebId 属性值(将其设置为子站点 ID),如下所示:

var lookupSchema = "<Field Type='Lookup' DisplayName='Magic' Required='FALSE' WebId='" + web.Id + "' List='" + techListID + "' ShowField='Title' StaticName='Magic' Name='Magic'/>";