Sharepoint Extension methods

public static class ListItemHelper
    {
        public static T GetValue<T>(this SPListItem item, string fieldName) where T : class
        {
            object o = item[fieldName];
            if (o == null || !(o is T)) return null;
            return (T)o;
        }
        public static Nullable<T> GetValue2<T>(this SPListItem item, string fieldName) where T : struct
        {
            object o = item[fieldName];
            if (o == null || !(o is T)) return null;
            return (Nullable<T>)(T)o;
        }
    }

Here is a small sample of how to use the methods:

SPListItem it = list.Items[0];

string title = it.GetValue<string>(“Title”);

DateTime? created = it.GetValue2<DateTime>(“Created”);

« Older entries