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:
C#:
-
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;
-
}
Secondly, we use the helper method to return the enumerable value in a nice generic format.
C#:
-
public static T ReturnEnumValue<T>(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);
-
}
Related posts:
- Getting a SharePoint Field Value In C#
- Insert Values Into The URL With C#
- SPView Field Comparison
- Getting Safe String Values
- Custom Field Validation With SPFieldValidationException
2 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

Very useful.
Comment by Danny — June 22, 2010 @ 10:42 am
[...] Getting SharePoint Enumerable Field Values [...]
Pingback by Links a lot, Sharepoint, Chartcontroll,xpath, SP Client Object Model « Just tinkering Blog — March 18, 2012 @ 7:49 am