Company      |       Articles & Research      |      Services      |      Software      |      Contact

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#:
  1. private static string NormalizeValue(string fieldValue)
  2. {
  3. if (!string.IsNullOrEmpty(fieldValue))
  4. {
  5. var str = fieldValue;
  6. if (str.StartsWith(SPFieldMultiChoiceValue.Delimiter))
  7. {
  8. str = str.Substring(SPFieldMultiChoiceValue.Delimiter.Length);
  9. }
  10. if (str.EndsWith(SPFieldMultiChoiceValue.Delimiter))
  11. {
  12. str = str.Substring(0, str.Length - SPFieldMultiChoiceValue.Delimiter.Length);
  13. }
  14. return str.Replace(SPFieldMultiChoiceValue.Delimiter, ", ");
  15. }
  16. return string.Empty;
  17. }

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

C#:
  1. public static T ReturnEnumValue<T>(SPListItem listItem, string fieldName, T defaultValue) where T : struct
  2. {
  3. var str = listItem[fieldName] as string;
  4. if ((str == null) || string.IsNullOrEmpty(str))
  5. {
  6. return defaultValue;
  7. }
  8. var normalized = NormalizeValue(str);
  9. return (T) Enum.Parse(typeof (T), normalized);
  10. }

share save 171 16 Getting SharePoint Enumerable Field Values

Related posts:

  1. Getting a SharePoint Field Value In C#
  2. Insert Values Into The URL With C#
  3. SPView Field Comparison
  4. Getting Safe String Values
  5. Custom Field Validation With SPFieldValidationException

2 Comments »

  1. Very useful.

    Comment by Danny — June 22, 2010 @ 10:42 am

  2. [...] 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

RSS feed for comments on this post. TrackBack URL

Leave a comment