Maping from SPList Field Types to .Net Types

When I am working with the WSS Object model, I generally do not pass references to SharePoint objects around. I usually create business objects to represent the lists that I am accessing. I will then use an Adapter pattern to map from th SharePoint object model to the custom business objects.

In the adapter class, you will need to extract data from fields/columns of the SPListItem. Depending on the types of columns you have, extracting that data is not as straight forward as you may expect. I've begun to build up a nice utility library for converting some of these field types. I thought it may be helpful to share some of it's functions.

The simplest column type is a text field. Getting data from such a field is pretty straight forward, except for the fact that if you leave the column empty during data entry, it actually ends up being stored as null. Instead of checking for null every time you need to map this type of field (which is very often), consider abstracting the logic out like I have:

        public static string GetStringField(SPListItem item, string key)
        {
            if (item[key] != null)
            {
                return item[key].ToString();
            }
            return "";
        }

Another common field type is the multi line text fields. These fields can store rich text or even html. Here are a couple of functions that will get the contents of the filed as HTML and as plain text.

        public static string GetMultiLineTextAsHTML(SPListItem item, string key)
        {

            SPFieldMultiLineText field = item.Fields[key] as SPFieldMultiLineText;
            if (field != null)
            {
                return field.GetFieldValueAsHtml(item[key]);
            }

            return "";
        }

        public static string GetMultiLineTextAsPlainText(SPListItem item, string key)
        {

            SPFieldMultiLineText field = item.Fields[key] as SPFieldMultiLineText;
            if (field != null)
            {
                return field.GetFieldValueAsText(item[key]);
            }

            return "";
        }

One of the more powerful column types is People and Groups. Getting the underlying SPUser, SPGroup or collection of SPUsers and SPGroups can be challenging. Check out these functions that make it easier.

        public static SPUser GetSPUser(SPListItem item, string key)
        {
            SPFieldUser field = item.Fields[key] as SPFieldUser;

            if (field != null)
            {
                SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
                if (fieldValue != null)
                {
                    return fieldValue.User;
                }
            }
            return null;
        }

        public static SPGroup GetSPGroup(SPListItem item, string key)
        {
            SPFieldUser field = item.Fields[key] as SPFieldUser;
            if (field != null)
            {
                SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
                if (fieldValue != null)
                {
                    string groupName = fieldValue.LookupValue;
                    return item.Web.SiteGroups[groupName];
                }
            }
            return null;
        }

        public static List<SPUser> GetSPUserCollection(SPListItem item, string key)
        {
            SPFieldUserValueCollection fieldValues = item[key] as SPFieldUserValueCollection;
            if (fieldValues != null)
            {
                List<SPUser> users = new List<SPUser>();
                foreach (SPFieldUserValue value in fieldValues)
                {
                    users.Add(value.User);
                }
                return users;
            }

            return null;
        }

Here is another function that will return the url to a file that is attached to a List Item. This also works for getting the url to images in a image library/picture gallery.

public static string GetItemAttachmentUrl(SPListItem item)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SPEncode.UrlEncodeAsUrl(item.Web.Url));
            sb.Append(@"/");
            sb.Append(SPEncode.UrlEncodeAsUrl(item.ParentList.RootFolder.Url));
            sb.Append(@"/");
            sb.Append(item.File.Name);
            return sb.ToString();
        }

Lastly I'll share a usefully little function that will return the value of a Lookup Column as a SPListItem object.

        public static SPListItem GetListItemFromLookup(SPListItem item, string lookupTableName, string key)
        {
            SPFieldLookup field = item.Fields[key] as SPFieldLookup;
            if (field != null)
            {
                SPFieldLookupValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldLookupValue;
                if (fieldValue != null)
                {
                    return item.Web.Lists[lookupTableName].GetItemById(fieldValue.LookupId);
                }
            }
            return null;
        }

I hope you find these useful. I do.

Happy Coding.