Wednesday, July 22, 2009

Parsing SharePoint delimited Multiple Value Lookup field values from web service results

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");

5 comments:

Edwin said...

Excellent Thanks.

Anonymous said...

the common approach for doing this would be to parse it as a SPFieldLookupValueCollection and then going through the items, although I'm not sure if that would be faster than using a regex

Tony DalPezzo said...

Maybe I should have clarified that the results where from the OOB webservice call (I did), either way the "ows_" in ows_MyMultipleSelectLookupField should have told you that. If I was using the API I would not need to create the dictionary or list.

Anonymous said...

How to add a item when i want to add multiple lookup values to the item.

Anonymous said...

Awesome, I've been looking for a Regex that actually works for the MultiLookupField - thx for posting this