Thought it would be helpful to share this.
As you know, SharePoint Document Library by default, if you just simply create one, are not turned on with Version Control. This could rather prone to human mistake or mis-configuration whenever you need to provision a new document library.
This is how I fulfill this.
Imagine i need to have the version control turn on, and with the following settings
What you need to do is to create a custom event receiver with List Added handler
Fire up your Visual Studio, create a SharePoint Farm solution and add your new event receiver
Select List Events and tick “A list was added”
In the ListAdded method, kindly do a checking before the code is firing, the below TemplateId 101 means Document Library
[sourcecode language=”csharp”]
if (properties.List != null && properties.TemplateId == 101 && properties.Web != null)
{
try
{
SPList docList = properties.List;//.Lists[properties.ListId];
docList.EnableVersioning = true;
docList.EnableMinorVersions = true;
docList.MajorVersionLimit = 3;
docList.MajorWithMinorVersionsLimit = 20;
//this.EventFiringEnabled = false;
docList.Update();
}
catch (Exception ex)
{
//Do something…
}
}
[/sourcecode]
If you do this, you will hit this error when creating a new document library
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
This is due to the Synchronisation mode for ListAdded event by default is “Asynchronous”.
You MUST DO the following
Open the Element.xml created via Visual Studio.
Add a “<Synchronization>Synchronous</Synchronization>” node into your receiver xml
[sourcecode language=”xml”]
<Receiver>
<Name>SharePointSettingEventReceiverListAdded</Name>
<Type>ListAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MOT.EventReceiver.SharePointSettingEventReceiver.SharePointSettingEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
</Receivers>
[/sourcecode]
Deploy your solution and hooray~