Tuesday, October 13, 2009

How to insert lookupfield in Sharepoint List.

Here is the code to get the lookup column value and then insert into list.


private static SPFieldLookupValue GetIteminLookUpList(SPWeb webObject, string lookupListName, string lookupString)
{
SPFieldLookupValue fieldLookupValue = null;
try
{
if (CheckListExist(webObject.Lists, lookupListName) == false)
{
// Throw error if the list doesn't exists.
throw new ApplicationException(string.Format("{0}'s list doesn't exists!", lookupListName));
}
SPList lookupList = webObject.Lists[lookupListName];
foreach (SPListItem item in lookupList.Items)
{
if (item["Title"].ToString() == lookupString)
{
fieldLookupValue = new SPFieldLookupValue(item.ID, lookupString);
break;
}
}
return fieldLookupValue;
}
catch (Exception ex)
{
throw new ApplicationException("Error: " + ex.Message);
}
}

No comments:

Post a Comment