Wednesday 17 February 2010

Setting Multiple Values of SPFieldLookup Column Programmatically ...

///
/// Set the values of a Lookup-Field with multiple values allowed.
///


public static void SetFieldValueLookup(this SPListItem item, string fieldName, IEnumerable lookupValues)
{
if (item != null)
{
SPFieldLookup field = item.Fields.GetField(fieldName) as SPFieldLookup;

SPFieldLookupValueCollection fieldValues = new SPFieldLookupValueCollection();

foreach (string lookupValue in lookupValues)
{
fieldValues.Add(GetLookupValue(item.Web, field, lookupValue));
}
item[fieldName] = fieldValues;
}
}


Example:

//Set the value of a SPFieldLookup field with multiple values allowed

string[] values = {"Hamburg", "London" };
item.SetFieldValueLookup("MultiLookupFieldName", values);

No comments:

Post a Comment