Updating all Look Up Column Values with Single Method in Share Point Programmatically


1. Consider we want to update two look up columns like Country and State in Share Point List


itemUpdate["CountryLookup"] = GetLookFieldIDS("InterColumnName", "ValuetoUpdateinlistitem", "ListName", SiteWebObject);

public static SPFieldLookupValueCollection GetLookFieldIDS(String coloumnIntName, String lookupValues, String lookupSourceList, SPWeb web)
{
SPFieldLookupValueCollection lookupIds = new SPFieldLookupValueCollection();

if (!String.IsNullOrEmpty(lookupValues))
{
SPList lookupList = web.Lists[lookupSourceList];

SPQuery query = new Microsoft.SharePoint.SPQuery();

query.Query = String.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq></Where>", coloumnIntName, lookupValues);

SPListItemCollection listItems = lookupList.GetItems(query);

foreach(Microsoft.SharePoint.SPListItem item in listItems)
{
SPFieldLookupValue value = new SPFieldLookupValue(item.ID, item.Title);
lookupIds.Add(value);
break;
}
}
return lookupIds;
}

Comments

Popular posts from this blog

SharePoint 2013 Keyword Query (KQL) Content Class Property Restrictions

Filtering the Sharepoint List Taxonomy Column using Rest API

SharePoint CSOM to Create Folders and Sub Folders based on Excel Data