When you query a list that contains a Lookup Column that allows multiple select values the value is returned like the following:
ows_MyMultipleSelectLookupField="1;#Foo;#2;#Foo Bar"
Yes, there’s that delimiter (;#) again.
The following two methods can help you get the Lookup values selected in the item.
/// <summary>
/// Remove the nasty delimiter
/// This can be used when returning multiple select lookup field values from a SharePoint list via a web service
/// Ex: ows_MyMultipleSelectLookupField="1;#Foo;#2;#Foo Bar"
/// returns Foo, Foo Bar
/// </summary>
/// <param name="sFieldValue"></param>
/// <returns>List of string values</returns>
public static List<string> GetDelimitedLookupValuesList(string sFieldValue)
{
List<string> results = new List<string>();
System.Text.RegularExpressions.Regex regexMultiValueField = new System.Text.RegularExpressions.Regex(@"(?<id>(\d+));[#](?<value>[(\w*)+\s]+)",
System.Text.RegularExpressions.RegexOptions.Compiled);
System.Text.RegularExpressions.Match choice = regexMultiValueField.Match(sFieldValue);
while (choice.Success)
{
results.Add(choice.Groups["value"].Value);
choice = choice.NextMatch();
}
return results;
}
/// <summary>
/// Remove the nasty delimiter
/// This can be used when returning multiple select lookup field values from a SharePoint list via a web service
/// Ex: ows_MyMultipleSelectLookupField="1;#Foo;#2;#Foo Bar"
/// returns {1,Foo} {2,Foo Bar}
/// </summary>
/// <param name="sFieldValue"></param>
/// <returns>Dictionay of id,value pairs</returns>
public static Dictionary<string, string> GetDelimitedLookupValuesDictionary(string sFieldValue)
{
Dictionary<string, string> results = new Dictionary<string, string>();
System.Text.RegularExpressions.Regex regexMultiValueField = new System.Text.RegularExpressions.Regex(@"(?<id>(\d+));[#](?<value>[(\w*)+\s]+)",
System.Text.RegularExpressions.RegexOptions.Compiled);
System.Text.RegularExpressions.Match choice = regexMultiValueField.Match(sFieldValue);
while (choice.Success)
{
results.Add(choice.Groups["id"].Value, choice.Groups["value"].Value);
choice = choice.NextMatch();
}
return results;
}
Usage:
//returns Foo, Foo Bar
List<string> myValues = GetDelimitedLookupValuesList("1;#Foo;#2;#Foo Bar");
//returns 1,Foo 2,Foo Bar
Dictionary<string,string> myValues = GetDelimitedLookupValuesDictionary("1;#Foo;#2;#Foo Bar");