Sivarajan's Blog

SharePoint | Office 365 | Azure | JavaScript

Generic List Helpers for .NET Client object model in SharePoint 2013/Office 365

Overview

We are using lot of helper methods in day to day activities for manipulating the SharePoint list data. In this post, we will see that how to play with Choice and Lookup Field.

Retrieving data using Choice Field (Multiple items)

In the choice field, we can add the list of string values or numeric values. But always it will return as object and then we will convert the value to array of object or string.

        public static string GetChoiceValues(object values)
        {
            List<string> results = new List<string>();

            if (values != null)
            {
                string[] arr = ((IEnumerable)values).Cast<object>()
                                 .Select(x => x.ToString())
                                 .ToArray();

                foreach (string value in arr)
                {
                    results.Add(value);
                }

                return string.Join(";", results.ToArray());
            }

            return string.Empty;
        }
  

 

Alternative Approach : Retrieving data using Choice Field (Multiple items)

Now we will write the same code in different way to retrieve the values using Generic type. So here we can avoid explicit type casting on each and every time.

        public static List<T> GetChoiceFieldValue<T>(object itemValue)
        {
            if (itemValue != null)
            {
                return ((IEnumerable)itemValue).Cast<object>()
                                 .Select(x => (T)Convert.ChangeType(x, typeof(T)))
                                 .ToList<T>();
            }

            return null;
        }

 

Usage:

List<double> values = ListHelper.GetChoiceFieldValue<double>(item["value1"]);

So we can use the same helper method for any data types.

Sometime, we may require the selected choice field values as string (comma separated), we will use the following code for that,

public static string GetChoiceFieldMultiValue(object itemValue, string delimiter)
        {
            if (itemValue != null)
            {
                List<string> values = GetChoiceFieldValue<string>(itemValue);

                if (values != null && values.Count() > 0)
                {
                    return string.Join(delimiter, values.ToArray());
                }

                return null;               
            }

            return null;
        }

Usage:

string value = ListHelper.GetChoiceFieldMultiValue(item["value1"], ";"); 

Retrieving data using Choice Field (Single item)

If the choice field has created with display option either Radio or Dropdown, we can use the following code to retrieve the selected single value from that.

public static T GetChoiceFieldSingleValue<T>(object itemValue)
        {
            if (itemValue != null)
            {
                return (T)Convert.ChangeType(itemValue, typeof(T));
            }

            return default(T);
        }

Retrieving data using Lookup Field (Allow Multiple Items)

Similarly, we will see how to retrieve the values from Lookup column if the lookup has created with multiple value selection option.

        public static List<T> GetLookupFieldValue<T>(object itemValue)
        {
            if (itemValue != null)
            {
                var itemValueArray = itemValue as Microsoft.SharePoint.Client.FieldLookupValue[];

                if (itemValueArray != null && itemValueArray.Count() > 0)
                {                   
                    return ((IEnumerable)itemValue).Cast<FieldLookupValue>()
                                 .Select(x => (T)Convert.ChangeType(x.LookupValue, typeof(T)))
                                 .ToList<T>();
                }
            }

            return null;
        }

Note: Always the LookupValue attribute will return the value as string.

Usage:

List<string> values = ListHelper.GetLookupFieldValue<string>(item["Value2"]);

We can also use the following method, suppose if we want to retrieve the values with keys.

       public static Dictionary<int, T> GetLookupFieldUserIdValue<T>(object itemValue)
        {
            List<string> results = new List<string>();

            if (itemValue != null)
            {
                var itemValueArray = itemValue as Microsoft.SharePoint.Client.FieldLookupValue[];

                if (itemValueArray != null && itemValueArray.Count() > 0)
                {
                    return ((IEnumerable)itemValue).Cast<FieldLookupValue>()
                        .ToDictionary(
                                    x => x.LookupId,
                                    x => (T)Convert.ChangeType(x.LookupValue, typeof(T)));                               
                }
            }

            return null;
        }

Usage:

Dictionary<int,string> values = ListHelper.GetLookupFieldKeyValue<string>(item["Value2"]);

Summary

We can use the same approach in Office 365 provider hosted App and will see how to work with other fields (Date, Managed Mata data, etc.) in the next post.

Download Source Code

Add comment

Loading