Thursday 8 September 2011

SPListItemVersionCollection , controlling Sharepoint 2010 versions programmatically

Microsoft Introduced a really useful version control for Sharepoint 2010, this version control is based in mayor versions (ie: 1.x,2.x,3.x …) and minor versions (ie: x.1, x.2, x.3 ….). When you upload a file for the very first time the version 1.0 is created, and from there you can start check in/out documents with the Web User Interface or Office 2010.

The best part of versioning it is the fact you can see your history and access to a particular version, and why not? restore it!.

The big problem we got with the versioning it is the database space. If someone does 200 versions of the same documents we will have 200 copies of the same document, so the performance in SQL 2008 will be lower. There are few ways to sort this out:

  • Via PowerShell
  • Web User Interface
  • Programmatically

We are going to focus in the last option, just because this blog is focus in pure Sharepoint Development and what we are trying to build it is a good backed for our farm.

I am going to post three snippets that you can add anywhere you want (Event Receivers, WorkFlows, Webparts etc…)

1- Restoring scenario and SPListItem.RestoreByLabel and SPListItem.RestoreByLabel , these two methods will allow us to restore documents by label or id, whatever we prefer. Because Sharepoint 2010 can only handle 3 copies the id will be 0,512 or 1024 . If we decide to use RestoreByLabel (my favourite) we will be able to pass a string with the version (ie: “3.0”). We have a document library with more than 100 items and everyone has 100 version…well someone wants to recover version 1.0 of every single document.

Snippet with label:

List<SPListItem> _aVersions = new List<SPListItem>();
Using (SPSite _sSite = new SPSite("http://xxxSHAREPOINT_SERVERxxx"))
{
     using (SPWeb _wWeb = _sSite.OpenWeb())
     {
           SPList _lDocumentList = _wWeb.Lists["Documents"];
           
           foreach (SPListItem item in _lDocumentList.Items)
           {
                _aVersions.Add(item.Versions.RestoreByLabel("1.0"));
           }
     }
}

Snippet with Id:

List<SPListItem> _aVersions = new List<SPListItem>();
Using (SPSite _sSite = new SPSite("http://xxxSHAREPOINT_SERVERxxx"))
{
     using (SPWeb _wWeb = _sSite.OpenWeb())
     {
           SPList _lDocumentList = _wWeb.Lists["Documents"];
           
           foreach (SPListItem item in _lDocumentList.Items)
           {
                _aVersions.Add(item.Versions.RestoreById(512));
           }
     }
}

2- Now we are going to see how to “Recycle” our versions and keep just the first one with SPListItemVersion.RecycleAll() . Having the same scenario, 100 documents with 100 versions let’s go to leave 100 documents with only one version:

Using (SPSite _sSite = new SPSite("http://xxxSHAREPOINT_SERVERxxx"))
{
     using (SPWeb _wWeb = _sSite.OpenWeb())
     {
           SPList _lDocumentList = _wWeb.Lists["Documents"];
           
           foreach (SPListItem item in _lDocumentList.Items)
           {
                item.Versions.RecycleAll();
           }
     }
}

3- Our last exercise will be to keep the id of the last version in a list.

List<string> _aVersions = new List<string>();
Using (SPSite _sSite = new SPSite("http://xxxSHAREPOINT_SERVERxxx"))
{
     using (SPWeb _wWeb = _sSite.OpenWeb())
     {
           SPList _lDocumentList = _wWeb.Lists["Documents"];
           
           foreach (SPListItem item in _lDocumentList.Items)
           {
                 foreach(SPListItemVersion _itemversion in item.Versions)
                 {
                        if (_itemversion.IsCurrentVersion)
                        {
                              _aVersions.Add(_itemversion.VersionId.ToString());
                        }
                 }
           }
     }
}

Conclusion: With the native version control in Sharepoint 2010 we secure one of the most important tasks in a Document Management System; House keeping. By tracing the performance every month for big farms we can activate some kind of methodologies with the users, like archiving very old versions of the documents and keeping the new ones, so the user experience if more agile.