SharePoint List Versioning

Utilizing SharePoint list gives us some key benefits instead of external list, there are paging, versioning, permission, etc. And at this time, I’m going to share about versioning, which enable us to store history of editing item inside list. Versioning by default is not active yet, we can enable it through List Setting. It seems a simple step to take but how if we have a system with many lists and all need versioning enabled. You can simply click this and click that thing for all day but I don’t recommend it.

There are some ways we could do to enable versioning on list. If you are developing solution with list definition, only left a few things to do are adding property VersioningEnabled in two files below.


In file Schema.xml:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="My Custom List" FolderCreation="FALSE" Direction="$Resources:core,Direction;"
      Url="Lists/MyCustomList" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/" VersioningEnabled="TRUE">

In file Element.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListTemplate
      Name="MyCustomListDefinition"
      VersioningEnabled="TRUE"
      Type="10001"
      BaseType="0"
      OnQuickLaunch="False"
      SecurityBits="11"
      Sequence="410"
      Image="/_layouts/images/itgen.png"/>
</Elements>

I’ve tried to submit versioning property on one file but not worked, so basically, we need a combination setting of both files, List element and schema file.

For existing list, we may enable list versioning by using code:

using (SPSite spsite = new SPSite("http://myserver/"))
{
   SPWeb spweb = spsite.OpenWeb();
   SPList list = spweb.Lists["MyCustomList"];
   list.EnableVersioning = true;
   list.Update();
 }

To retrieve versioning detail from sharepoint list, we could use:

using (SPSite spsite = new SPSite("http://myserver/"))
{
   SPWeb spweb = spsite.OpenWeb();
   SPListItemCollection listitems = spweb.GetList("http://myserver/Lists/MyCustomList").Items;
   foreach (SPListItem listitem in listitems)
   {
      foreach (SPListItemVersion versionItem in listitem.Versions)
      {
         Console.WriteLine("VersionId :" + versionItem.VersionId);
         Console.WriteLine("VersionLabel :" + versionItem.VersionLabel);
         Console.WriteLine("IsCurrentVersion :" + versionItem.IsCurrentVersion);
         Console.WriteLine("ListItem.Title :" + versionItem.ListItem.Title);
         Console.WriteLine("Created :" + versionItem.Created);
         Console.WriteLine("CreatedBy :" + versionItem.CreatedBy);
         Console.WriteLine("Level :" + versionItem.Level);
      }
   }
 }

Simple isn’t it? Utilize versioning, we possibly recheck history’s details of listitem and restore any version. Bear in mind that versioning also need additional space in sharepoint database, so please considering which list should have versioning enabled.

See more in sptechnet.

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