Getting SharePoint Enumerable Field Values

More a quick tip than anything. In the below are two helper methods that make the process a lot easier. The first method will help to normalize the values for handling things like delimiters:

[csharp]
private static string NormalizeValue(string fieldValue)
{
if (!string.IsNullOrEmpty(fieldValue))
{
var str = fieldValue;
if (str.StartsWith(SPFieldMultiChoiceValue.Delimiter))
{
str = str.Substring(SPFieldMultiChoiceValue.Delimiter.Length);
}
if (str.EndsWith(SPFieldMultiChoiceValue.Delimiter))
{
str = str.Substring(0, str.Length – SPFieldMultiChoiceValue.Delimiter.Length);
}
return str.Replace(SPFieldMultiChoiceValue.Delimiter, “, “);
}
return string.Empty;
}

[/csharp]

Secondly, we use the helper method to return the enumerable value in a nice generic format.

[csharp]

public static T ReturnEnumValue(SPListItem listItem, string fieldName, T defaultValue) where T : struct
{
var str = listItem[fieldName] as string;
if ((str == null) || string.IsNullOrEmpty(str))
{
return defaultValue;
}
var normalized = NormalizeValue(str);
return (T) Enum.Parse(typeof (T), normalized);
}
[/csharp]

Share

One Comment

  1. Danny says:

    Very useful.

Trackbacks/Pingbacks

  1. Links a lot, Sharepoint, Chartcontroll,xpath, SP Client Object Model « Just tinkering Blog - [...] Getting SharePoint Enumerable Field Values [...]

Leave a Reply

Your email address will not be published. Required fields are marked *