//To Read LookUp Field
public static string ReadLookupValue(SPListItem item, string columnName)
{
SPFieldLookupValue lookupField = new SPFieldLookupValue(item[columnName].ToString());
return lookupField.LookupValue;
}
//To Update LookUp Field
int ListItemID = GetListItemID("ListWithLookUpColumn","ValueToBeUpdate");
ListItem[LookUpFieldColumnName] = ListItemID;
ListItem.Update();
/////
public static int GetListItemID(string ListName, string ListColumnValue)
{
int ListItemID=0;
try
{
SPSite Site = new SPSite(SITE_URL);
SPWeb Web = Site.OpenWeb();
SPList List = Web.Lists[ListName];
SPListItemCollection ListItems = List.Items;
int ListItemCount = ListItems.Count;
for (int i = 0; i < ListItemCount; i++)
{
SPListItem ListItem = ListItems[i];
if (ListItem["Title"].ToString() == ListColumnValue)
{
ListItemID =Convert.ToInt32(ListItem["ID"].ToString());
break;
}
}
return ListItemID;
}
catch (Exception ex)
{
return 0;
}
}
No comments:
Post a Comment