Read Values From Multi Value Lookup And Choice Field In SharePoint List

Ashok Raja
 
Solutions Architect
December 6, 2012
 
Rate this article
 
Views
3960

This utility class for SharePoint helps you to read / parse data from Multi Value / Single Value look up and choice fields. This utility class cleans up the delimiter used by SharePoint while rendering lookup field ,choice field and returns the value as string, array , list and Dictionary.

As an alternative approach, extension methods for SPListItem class can also be created to extract values from lookup and choice fields. We can see that option in the later part of this article.

Option 1 – Conventional Helper Methods to extract Lookup and Choice Field Values

Object Structure

MultiValueChoiceAndLookUp

Source Code

 using Microsoft.SharePoint;
 using System;
 using System.Collections.Generic;
 
 namespace SFS.SharePoint.Helpers
 {
     /// <summary>
     /// SharePoint Frontier's Utility class to parse Look Up Field and Choice Field values by Ashok Raja .T
     /// </summary>
     public static class FieldDataParser
     {
         private const string delimiter = ",";
         #region Get Look Up Field Values
         /// <summary>
         /// Get look up values as list.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static List<string> GetValuesAsList(SPListItem item, string ColumnName)
         {
             List<string> Items = new List<string>();
             if (item[ColumnName] != null)
             {
                 SPFieldLookupValueCollection LookUpItemCollection = new SPFieldLookupValueCollection(item[ColumnName].ToString());
                 for (int i = 0; i < LookUpItemCollection.Count; i++)
                 {
                     SPFieldLookupValue Item = LookUpItemCollection[i];
                     Items.Add(Item.LookupValue);
                 }
             }
             return Items;
         }
 
         /// <summary>
         /// Get look up values as array.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static string[] GetValuesAsArray(SPListItem item, string ColumnName)
         {
             return GetValuesAsList(item, ColumnName).ToArray();
         }
 
         /// <summary>
         /// Get look up values as string.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static string GetValuesAsString(SPListItem item, string ColumnName)
         {
             return string.Join(delimiter, GetValuesAsList(item, ColumnName));
         }
 
         /// <summary>
         /// Get look up values as dictionary.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static Dictionary<int, string> GetValuesAsDictionary(SPListItem item, string ColumnName)
         {
             Dictionary<int, string> Items = new Dictionary<int, string>();
             if (item[ColumnName] != null)
             {
                 SPFieldLookupValueCollection LookUpItemCollection = new SPFieldLookupValueCollection(item[ColumnName].ToString());
                 for (int i = 0; i < LookUpItemCollection.Count; i++)
                 {
                     SPFieldLookupValue Item = LookUpItemCollection[i];
                     Items.Add(Item.LookupId, Item.LookupValue);
                 }
             }
             return Items;
         } 
         #endregion
 
         #region Get Look Up Field Ids
         /// <summary>
         /// Get ids as list.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static List<int> GetIdsAsList(SPListItem item, string ColumnName)
         {
             List<int> Items = new List<int>();
             if (item[ColumnName] != null)
             {
                 SPFieldLookupValueCollection LookUpItemCollection = new SPFieldLookupValueCollection(item[ColumnName].ToString());
                 for (int i = 0; i < LookUpItemCollection.Count; i++)
                 {
                     SPFieldLookupValue Item = LookUpItemCollection[i];
                     Items.Add(Item.LookupId);
                 }
             }
             return Items;
         }
 
         /// <summary>
         /// Get ids as array.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static int[] GetIdsAsArray(SPListItem item, string ColumnName)
         {
             return GetIdsAsList(item, ColumnName).ToArray();
         }
 
         /// <summary>
         /// Get ids as string.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static string GetIdsAsString(SPListItem item, string ColumnName)
         {
             return string.Join(delimiter, GetIdsAsList(item, ColumnName));
         }
 
         #endregion
 
         #region Get Choice Field Values
         /// <summary>
         /// Get choice field values as list.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static List<string> GetChoiceAsList(SPListItem item, string ColumnName)
         {
             string[] StringItems = GetChoiceAsArray(item, ColumnName);
             List<string> Items = new List<string>();
             if (StringItems != null && StringItems.Length > 0)
                 Items.AddRange(GetChoiceAsArray(item, ColumnName));
             return Items;
         }
 
         /// <summary>
         /// Get choice field values as array.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static string[] GetChoiceAsArray(SPListItem item, string ColumnName)
         {
             List<string> Items = new List<string>();
             if (item[ColumnName] != null)
             {
                 return item[ColumnName].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
             }
             else
                 return null;
         }
 
 
         /// <summary>
         /// Get choice field value as string.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static string GetChoiceAsString(SPListItem item, string ColumnName)
         {
             string[] Items = GetChoiceAsArray(item, ColumnName);
             if (Items == null || Items.Length == 0)
                 return string.Empty;
             else
                 return string.Join(delimiter, Items);
         } 
         #endregion
 
     }
 }

How to Invoke

 // City is display name of Field
 string ValueAsString = SFS.SharePoint.Helpers.FieldDataParser.GetValuesAsString(item, "City");
 //Code to handle data
 
 string[] ValueAsArray = SFS.SharePoint.Helpers.FieldDataParser.GetValuesAsArray(item, "City");
 foreach (string str in ValueAsArray)
 {
     // Code to handle data
 }
 
 List<string> ValueAsList = SFS.SharePoint.Helpers.FieldDataParser.GetValuesAsList(item, "City");
 
 foreach (string lstItem in ValueAsList)
 {
     // Code to handle data
 }
 
 Dictionary<int, string> ValuesAsDictionary = SFS.SharePoint.Helpers.FieldDataParser.GetValuesAsDictionary(item, "City");
 foreach (var dictItem in ValuesAsDictionary)
 {
     int key = dictItem.Key;
     string value = dictItem.Value;
     // Code to handle data
 }

Option 2 – SPListItem Extension Methods to read Lookup and Choice Field Values

To create an extension method for SPListItem a small change to the functions declared in option 1 is required.  Instead of directly passing list item as a parameter  to the function we use the key word this as a part of the parameter to convert the ordinary parameter to an extension. Another mandatory requirement is to have a static class to hold all these extension methods.

Object Structure

MuliValueEx

Source Code (Partial)

 using Microsoft.SharePoint;
 using System;
 using System.Collections.Generic;
 
 namespace SFS.SharePoint.Helpers
 {
     /// <summary>
     /// SharePoint Frontier's ListItem Extension class to parse Look Up Field and Choice Field values by Ashok Raja .T
     /// </summary>
     public static class FieldDataParserEx
     {
         private const string delimiter = ",";
         #region Get Look Up Field Values
         /// <summary>
         /// Get look up values as list.
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="ColumnName">Name of the column.</param>
         /// <returns></returns>
         public static List<string> GetValuesAsList(this SPListItem item, string ColumnName)
         {
             List<string> Items = new List<string>();
             if (item[ColumnName] != null)
             {
                 SPFieldLookupValueCollection LookUpItemCollection = new SPFieldLookupValueCollection(item[ColumnName].ToString());
                 for (int i = 0; i < LookUpItemCollection.Count; i++)
                 {
                     SPFieldLookupValue Item = LookUpItemCollection[i];
                     Items.Add(Item.LookupValue);
                 }
             }
             return Items;
         }
     .
     .
     .
     .
     .
 
     }
 }

How to Invoke

To invoke this extension methods , import the name space SFS.SharePoint.Helpers and follow the below calling convention to invoke the extension methods

 // City is display name of Field
 string ValueAsStringEx = item.GetValuesAsString("City");
 // Code to handle data
 
 string[] ValueAsArrayEx = item.GetValuesAsArray("City");
 foreach (string str in ValueAsArrayEx)
 {
     // Code to handle data
 }
 
 List<string> ValueAsListEx = item.GetValuesAsList("City");
 
 foreach (string lstItem in ValueAsListEx)
 {
     // Code to handle data
 }
 
 Dictionary<int, string> ValuesAsDictionaryEx = item.GetValuesAsDictionary("City");
 foreach (var dictItem in ValuesAsDictionaryEx)
 {
     int key = dictItem.Key;
     string value = dictItem.Value;
     // Code to handle data
 }

Download Source

Author Info

Ashok Raja
 
Solutions Architect
 
Rate this article
 
I am Ashok Raja, Share Point Consultant and Architect based out of Chennai, India. ...read more
 

Leave a comment