SharePoint In Action

An attempt to share my day-to-day SharePoint experience

Archive for the month “July, 2012”

Programmatically Read Versions Of A SharePoint List Item

Here I will show how you can read all versions of a list item programmatically. As you know, in SharePoint you can turn on the versioning for a list so whenever you modify an item in your list, it keeps track of all your changes so you can revert back to a specific version.

To Enable the Versions for a Sharepoint list, you’d need to go to “List Settings” and go to “Versioning Settings” and then change the value of “Create a version each time you edit an item in this list?” to Yes.

To better demonstrate this, imagine we have a list item as below:

Employee Vacation Days Comment
Person or Group Single Line of Text Single Line of Text

We turn on the versioning for the list and make the comment field mandatory. So whenever someone modifies the list item, they have to leave a comment and versioning will take care of the rest.

Now we want to read all the comments for an item and show the history in a grid let’s say. In my code I have created a simple class called “Comment“:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;</code>

namespace Test
    public class Comment
        private string body ;
        private int order;
        private DateTime date;
        private SPUser author;

         #region Properties

        internal SPUser Author
            get { return author; }
            set { author = value; }

        internal DateTime Date
            get { return date; }
            set { date = value; }

        internal string Body
            get { return body; }
            set { body = value; }

        internal int Order
            get { return order; }
            set { order = value; }

         #region Constructors

        internal Comment(): base()
            this.order = 0;
   = SPContext.Current.Web.CurrentUser;
            this.body = string.Empty;
   = DateTime.Now.Date;


and here is a simple method that loops through the versions of an item. Please note that I am passing the item to my method as well as using “SPListItemVersionCollection”:

private List&lt;Comment&gt; EmployeeTimeOffDays(SPListItem item) 
         List&lt;Comment&gt; cmnts = new List&lt;Comment&gt;();
         if ((item.Versions!= null) &amp;&amp; (item.Versions.Count &gt; 0))
                     SPListItemVersionCollection versions = item.Versions;
                     for (int i = 0; i &lt; versions.Count - 1; i++)
                            if (versions[i]["Comment"] != null)
                                     Comment cmnt = new Comment();
                                     cmnt.Body = versions[i]["Comment"].ToString();
                                     cmnt.Author = (SPUser)(versions[i].ListItem, "Created By");
                                     cmnt.Date = DateTime.Parse(versions[i]["Created"].ToString());
      return cmnts;

Nader Heshmat


Post Navigation