In this post, I will explain how to Get and Set a SharePoint Multiple Lookup Field Value programmatically using C#?
In this section, we will show how to get and set SharePoint Multiple Lookup Field value in SharePoint List using C# (Server Side Object Model).
Consider, you have a SharePoint List with a Lookup (Allow Multiple Values) field and you would like to update and set the SharePoint Multiple Lookup Field Value programmatically using C#.
In this case, you should use the below “setMultipleLookupFieldValue()” function with your List name and field name to can set Lookup (Allow Multiple Values) field Value using C#.
C# Code
public void setMultipleLookupFieldValue() {
using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
using(SPWeb web = site.OpenWeb()) {
SPList list = web.Lists["Users"];
SPListItem item = list.GetItemById(1);
SPFieldLookupValueCollection itemValues = SPFieldLookupValueCollection();
itemValues.Add(new SPFieldLookupValue(1, "Title"));
item["FieldName"] = itemValues;
item.Update();
}
}
}
Download the full code for SharePoint Lookup Field Operations Solution in C#
In the previous section, we have learned how to set Multiple Lookup Field Value programmatically using C#, In this section, we will show how to get SharePoint Multiple Lookup Field Value Using C#.
C# Code
public void setMultipleLookupFieldValue() {
using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
using(SPWeb web = site.OpenWeb()) {
SPList list = web.Lists["Users"];
SPListItem item = list.GetItemById(1);
SPFieldLookupValueCollection itemValues = item["FieldName"]
as SPFieldLookupValueCollection;
foreach (SPFieldLookupValue itemValue in itemValues)
{
int id = itemValue.LookupId;
string value = itemValue.LookupValue;
}
}
}
}
It’s strongly recommended to read Work with SharePoint Lookup Field Programmatically
Conclusion
In conclusion, we have learned how to Get and Set a SharePoint Multiple Lookup Field value Programmatically using Server Object Model C#?
Download
Applies To
- SharePoint 2016.
- SharePoint 2013.
- SharePoint 2010.
- C# SSOM.
You might also like to read
- Get and Set a SharePoint Lookup Field Value Using Server Object Model C#.
- Get and Set a SharePoint User Field Value Using Server Object Model C#.
- Get and Set a SharePoint Multiple Choice Field Value Using Server Object Model C#.
- Retrieve a SharePoint Choice Field Value Using Server Object Model C#.
Have a Question?
If you have any related questions, please don’t hesitate to ask it at deBUG.to Community.
Pingback: Get SharePoint Choice Field Value In C# | SPGeeks
Pingback: Bind dropdown from SharePoint List in C# | SPGeeks
Pingback: Get and Set SharePoint Multiple Choice Field Value in C# | SPGeeks
Pingback: Get and Set SharePoint User Field Value in C# | SPGeeks
Pingback: How to get Title and URL of SharePoint Page via Object Model | SPGeeks