【问题标题】:SharePoint Lookup ListItem not loading valueSharePoint Lookup ListItem 未加载值
【发布时间】:2023-03-31 05:03:01
【问题描述】:

我正在将一个数据库导入 SharePoint 列表。我有一份公司拥有的工厂列表。

工厂具有“工厂名称”、“公司”、“州”、“地址”等列。 “工厂名称”和“地址”都是单个文本行,但“公司”和“州”是查找类型。 “公司”列应在“公司”列表中查找名称并链接到它。链接到“州”列表的“州”列也是如此。

这是我的代码的 sn-p:

SP.ClientContext context = new SP.ClientContext("http://localhost");

...

newPlantLocation["Company"] = GetItemId(context, "Title", companyName, "Companies");
newPlantLocation["State"] = GetItemId(context, "Title", plantState, "States");
newPlantLocation["Title"] = plantName;
newPlantLocation["Address_x0020_Line_x0020_1"] = plantAddressLine1;

...

newPlantLocation.Update();

这里是返回项目 ID 的函数,它不是最有效但很容易理解:

private static int GetItemId(SP.ClientContext context, string columnName, string displayName, string listName)
{
    SP.List list = context.Web.Lists.GetByTitle(listName);
    SP.CamlQuery query = new SP.CamlQuery();
    SP.ListItemCollection items = list.GetItems(query);

    context.Load(items);
    context.ExecuteQuery();

    foreach (SP.ListItem listItem in items)
    {
        if (listItem[columnName].Equals(displayName))
        {
            return listItem.Id;
        }
    }
    return 0;
}

我遇到的问题是,当进口商运行时,植物填充“植物”列表,但“公司”字段留空。我很困惑,因为“状态”字段的完成方式几乎相同,并且最终以某种方式填充。我已经对其进行了调试,GetItemId 函数返回了正确的公司整数,只是没有显示或保存到项目中。

如果我执行以下操作,它会突然开始工作:

newPlantLocation["Company"] = 4;

【问题讨论】:

  • 您是否检查过 ULS 的异常情况?也许有什么东西阻止了事件处理程序在 Company 字段上触发更新。

标签: c# sharepoint sharepoint-list sharepoint-clientobject


【解决方案1】:

我现在解决了。

这行 context.ExecuteQuery(); 似乎造成了麻烦,我在更新 ListItem 之前过早地执行了查询。

所以我在做任何事情之前更改了我的 GetItemId 函数以更新 ListItem。

private static int GetItemId(SP.ClientContext context, string columnName, string displayName, string listName, SP.ListItem thisItem)
{
    thisItem.Update();

    //this rest of the code is the same
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多