Freigeben über


Parsing multi-value fields - MultiChoice, Lookup, User, URL. Rules for the ";#" delimiter.

For certain types of columns (aka fields), SharePoint stores several values in one field, using a delimiter to separate the different values.

Column Type

Items

Delimiter

Example

Choices , configured as Display choices using Checkboxes (allow multiple selections )

All selected choices

;#

;#First;#Second;#Third;#

Lookup

Item index, lookup value

;#

1;#First

Person or Group

Item index, user value

;#

42;#Mark Arend

Hyperlink or Picture

URL, text link

,

https://microsoft.com, Microsoft

 

Whenever parsing’s involved, I like to use regular expressions, even if I’m just extracting a single substring. This way, I can maintain all my parsing instructions in one place, making it easy to fix problems like missing a boundary case in my original understanding of the parse.

Below are three regular expressions that parse the above column types. I usually make these static members of a class named Util, so I can call them easily throughout my code. The code samples include commented examples of using each regular expression.

/// <summary>

/// Regular expression to isolate multi-choice values

/// </summary>

/// <example>

// System.Text.RegularExpressions.Match choice;

// string strField;

// foreach (SPListItem item in list.Items)

// {

// strField = (string)item["MultiChoiceField"];

// output.Append(item.Title + ": " + strField + "<BR>");

// if (strField != null)

// {

// choice = Util.rexMultiChoiceField.Match(strField);

// while (choice.Success)

// {

// output.Append("- " + choice.Result("$1") + "<BR>");

// choice = choice.NextMatch();

// }

// }

// }

///// </example>

internal static System.Text.RegularExpressions.Regex rexMultiChoiceField =

new System.Text.RegularExpressions.Regex(@"#(.+?);",

System.Text.RegularExpressions.RegexOptions.Compiled);

/// <summary>

/// Regular expression to isolate an ID ("$1") or lookup value ("$2")

/// Use this for "Lookup" fields, or for "Person or Group" fields

/// </summary>

/// <example>

///    string LookupField, LookupId, LookupValue;

///    if (Util.rexLookupField.Match(LookupField).Success)

///    {

///        LookupId = Util.rexLookupField.Match(LookupField).Result("$1");

///        LookupValue = Util.rexLookupField.Match(LookupField).Result("$2");

///    }

/// </example>

internal static System.Text.RegularExpressions.Regex rexLookupField =

new System.Text.RegularExpressions.Regex(@"(\d+);#(.*)$",

System.Text.RegularExpressions.RegexOptions.Compiled);

/// <summary>

/// Regular expression to isolate a URL ("$1") or its Description ("$2")

/// </summary>

/// <example>

///    string UrlField, UrlPath, UrlName;

///    if (Util.rexUrlField.Match(UrlField).Success)

///    {

///        UrlPath = Util.rexUrlField.Match(UrlField).Result("$1");

///        UrlName = Util.rexUrlField.Match(UrlField).Result("$2");

///    }

/// </example>

internal static System.Text.RegularExpressions.Regex rexUrlField =

new System.Text.RegularExpressions.Regex(@"^(.*), +(.*)$",

System.Text.RegularExpressions.RegexOptions.Compiled);

 

See also the SPListItem Class discussion in the SDK for additional samples. If you know of better details regarding these delimited value fields, please post replies. Thanks!!

Comments