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”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: