The example method or function accepts two parameters. The first one is SPList which is equal to your lookup list and the second one is a string which is equal to the name of your lookup item. This method will find the item from the lookup list by using Name. If the item is found, it will be used in creating SPFieldLookupValue category using the item id & item title. SPFieldLookupValue category will then be returned by the method and it can be used now as a value of the lookup field.
static
SPFieldLookupValue GetCategory(SPList list, string
lookupitemName)
{
SPFieldLookupValue category = null;
//get id from
Categories List
for (int ItemNum = 0; ItemNum < list.Items.Count;
ItemNum++)
{
SPListItem listItem =
list.Items[ItemNum];
if
(Convert.ToString(listItem["Title"]).Trim().ToLower() ==
lookupitemName.Trim().ToLower())
{
category = new SPFieldLookupValue(listItem.ID, listItem.Title);
break;
}
}
return
category;
}
Here is the sample code on how to use the above method:
listitem["Category"]
= GetCategory(CategoriesList, "Admin
Matters");
No comments:
Post a Comment