tags:

views:

1218

answers:

2

How can I modify a SharePoint site so that versioning is turned on by default in Document Libraries?

+3  A: 

Versioning is not done at the site level, but at the list level.

If you want versioning to be turn on on each new library, you'll have to either:

  • Use your own library template (with versioning turned on)
  • Use feature + event handler to programmatically activate versioning on each new list

The easiest way is probably to use your own template. To do this, create a new document library, activate versioning, then save this list as template.

When you create a new list, you will then be able to use your template and directly create a list with versioning activated.

Nico
+3  A: 

You could of course create your own site definition, but that's probably not the best solution. Creating a custom library template will work too, but if you want versioning turned on for the libraries that a particular site definition creates for you, you'll have to come up with something else.

We happen to have done this for our SharePoint implementation. We decided the best way was to create an event handler feature and staple it to all sites so that when the site is created, versioning will get turned on for all existing document libraries. Of course, new document libraries would get whatever versioning options the user who created it set.

The problem we ran into is that there is no "ListCreating" event handler so we couldn't turn the versioning on at that point. So, we tried to put the code inside the FeatureActivated event handler, figuring it would be activated on site creation and then all document libraries could be changed to have versioning turned on. The problem is that this event fired before the libraries were actually created.

So instead, we decided to put the code into the "ItemAdding" event handler and remove it after the first time that it runs. So the first time a user adds a list item or a document, it will turn on versioning for all document libraries in the site. This way, we ensure there is no way for a user to add a document to an existing library without it being versioned. Additionally, any libraries that get created before an item gets added will have versioning turned on by default as well.

It was a bit of a hairy solution, but it has worked very well for us. Here's the code we used:

public class SetVersioning : SPItemEventReceiver
{
    public override void ItemAdding(SPItemEventProperties properties)
    {
        SPWeb CurrentWeb = properties.OpenWeb();
        foreach (SPDocumentLibrary doclib in CurrentWeb.GetListsOfType(SPBaseType.DocumentLibrary))
        {
            doclib.EnableVersioning = true;
            doclib.MajorVersionLimit = 8;
            //doclib.EnableMinorVersions = true;
            doclib.Update();
        }
        //now get rid of the receiver
        SPEventReceiverDefinitionCollection receivers = CurrentWeb.EventReceivers;
        foreach (SPEventReceiverDefinition definition in receivers)
        {
            if (definition.Name.Equals(EVENT_RECEIVER_NAME))
            {
                definition.Delete();
                break;
            }
        }

        base.ItemAdding(properties);
    }
}
Bryan Friedman