Programmatically Enable Versioning to a Sharepoint List

We often have a requirement to create a list using the SharePoint object model and at the same time we have to enable the versioning settings.

This can be achieved by navigating to the list settings but what if we want to enable it through the object model only.

In this article we will see how we can enable the versioning programmatically.

  1. Change the AllowUnsafeProperty of the web to true so that SharePoint will allow to change the properties of the list.
  2. Get the list in SPlist Object.
  3. Set the properties accordingly.

The following is the code snippet which is used to do the versioning settings.

/***************************************************************/

        ///Enable Versioning to lists

        ///

        SPContext.Current.Web.AllowUnsafeUpdates = true;

        SPList objList = SPContext.Current.Web.Lists["TestDoc"];

 

        //EnableVersioning:- True if versioning is enabled for the document library; Otherwise false.

        objList.EnableVersioning = true;

        //EnableMinorVersions:- true if minor versions are enabled when versioning is enabled for the document library; otherwise false.

        objList.EnableMinorVersions = true;

        //EnableModeration: property is true if Content Approval is enabled; otherwise, false

        objList.EnableModeration = true;

        //ForceCheckout:- This property is true if Forced checkout is enabled for the document library; otherwise false

        objList.ForceCheckout = true;

        //DraftVersionVisibility:- Set the accessibilty of the minor versions of document drafts within the list.

        objList.DraftVersionVisibility = DraftVisibilityType.Author;

        objList.Update();

  SPContext.Current.Web.AllowUnsafeUpdates = false;

        /****************************************************************/

In this we can play with the versioning settings for a list.

Up Next
    Ebook Download
    View all
    Learn
    View all