Wednesday, September 22, 2010

Turn on versioning on a site's Document Libraries.

We had a lot of sites that needed to have versioning turned on, here's a simple console app I wrote to do this, you just pass the url of the web as an argument:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Mirant.Utilities.SiteVersioningActivator
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
                throw new Exception("URL of the site is required as a parameter");


            string webUrl = args[0];

            using (SPSite site = new SPSite(webUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    foreach (SPDocumentLibrary docLib in web.GetListsOfType(SPBaseType.DocumentLibrary))
                    {
                        docLib.EnableVersioning = true;
                        docLib.MajorVersionLimit = 5;
                        docLib.Update();
                    }
                }
            }
        }
    }
}

No comments: