以编程方式从查找字段值获取 ID 的更好方法是什么

本文关键字:ID 更好 方法 是什么 获取 方式 编程 查找 字段 | 更新日期: 2023-09-27 18:35:04

当我尝试从以下位置获取id时:

string idValue = item[Lookup].ToString();

我通过示例获得下一个值:

1

;#1

我需要这样值:

1

实际上,此代码处理需求:

using (SPSite site = new SPSite(context.CurrentWebUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        //Context list                 
        SPList list = web.Lists[context.ListId];
        SPList child = web.Lists[List]; 
        SPListItem currentItem = list.GetItemById(context.ItemId);
        string updateItems = "";
        int ID = currentItem.ID;
        foreach (SPListItem item in child.Items)
        {
            string idValue = item[Lookup].ToString();
            int partial = idValue.LastIndexOf(";");
            string idPure = idValue.Substring(0, partial);
            if (idPure == ID.ToString())
            {
                item[Field] = Value;
                item.Update();
                updateItems += item.ID.ToString();
            }
        }              
        //Return Items*/
        results["Items"] = updateItems;
        SPWorkflow.CreateHistoryEvent(web, context.WorkflowInstanceId, 0,
            web.CurrentUser, TimeSpan.Zero, "Information",
            "Event from sandboxed, updates: " + updateItems, string.Empty);
    }
}

我想知道一个更好的函数或属性来从查找字段中获取 ID。

以编程方式从查找字段值获取 ID 的更好方法是什么

SPFieldLookupValue fieldLookupValue = new SPFieldLookupValue(item["FieldName"].ToString());
int lookupID = fieldLookupValue.LookupId;

你来了:)

    SPList mySPList = oWeb.Lists["ProjectList"];
newItem["LookupFieldName"] = new SPFieldLookupValue(getLookUp(mySPList,LookupFieldValue), LookupFieldValue);

public static int getLookUp(SPList oList, string FieldValue, string sFieldName="Title")
        {
            foreach (SPListItem spi in oList.GetItems())
            {
                if (spi[sFieldName].ToString() == FieldValue)
                {
                    return spi.ID;
                }
            }
            return 0;
        }

我可以使用此代码在 SharePoint 中设置查找字段

<script>
window.onload = (event) => {
document.getElementById("tafahomNameId_78ec7c44-beab-40de-9326-095f474519f4_$LookupField").value = 1;
};
</script>