Recently, we finally migrated all our content to SharePoint Online (Office365). Microsoft really did a great job with their infrastructure and optimizations. SharePoint works great in the cloud.

When deploying our new intranet portal I defined the following rule for Document Libraries:

  • Each should be visible in Quick Launch
  • Major versions should be enabled

You can achieve this:

  • By manually configuring each document library (Please don’t.)
  • By creating a document library template with these settings (it will work, but if someone uses default template, it will not follow your guidelines)
  • By automating this via .NET code or PowerShell

For our internal use I developed a small C# app that uses SharePoint Client Object Model to configure the settings mentioned above. My friend Wictor Wilen has posted this great Office365 / SharePoint Online authentication library that saved me a lot of time.

The sample below will iterate your sites and update Document Library settings to match the rule I mentioned above. You will also need the library from Wictor.

Please note: I have no idea if this code is optimal. It might break your SharePoint. It worked well for our, English template based site collection.

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

namespace Acceleratio.Office365.DLUpdates {
    class Program {
        static void Main(string[] args) {

            Console.Write("SharePoint Online URL (e.g. https://contoso.sharepoint.com): ");
            string url = Console.ReadLine();
            Console.Write("Username (e.g. billg@contoso.onmicrosoft.com): ");
            string username = Console.ReadLine();
            Console.Write("Password: ");
            string password = ReadPassword();

            // download here: http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx
            Wictor.Office365.MsOnlineClaimsHelper claimsHelper = new Wictor.Office365.MsOnlineClaimsHelper(url, username, password);
            using (ClientContext context = new ClientContext(url))
            {
                context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;

                context.Load(context.Web);
                context.ExecuteQuery();

                Console.WriteLine("Please wait while we update your site. This can take a while... Go for a run!");
                GetSubSites(context, context.Web);
            }
        }

        private static void GetSubSites(ClientContext context, Web web)
        {
            Console.WriteLine("Updating {0}", web.ServerRelativeUrl);
            UpdateDocumentLibrarySettings(context, web);

            context.Load(web.Webs);
            context.ExecuteQuery();

            foreach(Web w in web.Webs)
            {
                GetSubSites(context, w);
            }
        }

        private static void UpdateDocumentLibrarySettings(ClientContext context, Web web)
        {
            context.Load(web.Lists);
            context.ExecuteQuery();

            ListCollection lists = web.Lists;
            foreach (List list in lists)
            {
                context.Load(list.ContentTypes);
                context.Load(list, l => l.IsSiteAssetsLibrary);
                context.ExecuteQuery();

                // Only non hidden, non system Document Libraries will be updated (update for different languages)
                if (!list.Hidden && !list.IsSiteAssetsLibrary && list.BaseType == BaseType.DocumentLibrary && list.Title != "Form Templates" && list.Title != "Style Library")
                {
                    list.EnableVersioning = true;

                    // Site Pages lib will not be visible on Quick Launch (update for different languages)
                    if(list.Title != "Site Pages")
                    {
                        list.OnQuickLaunch = true;
                    }

                    list.Update();
                }
            }
        }

        private static string ReadPassword()
        {
            string pass = "";
            ConsoleKeyInfo key;

            do
            {
                key = Console.ReadKey(true);

                // Backspace Should Not Work
                if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
                {
                    pass += key.KeyChar;
                    Console.Write("*");
                }
                else
                {
                    if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
                    {
                        pass = pass.Substring(0, (pass.Length - 1));
                        Console.Write("\b \b");
                    }
                }
            }
            // Stops Receving Keys Once Enter is Pressed
            while (key.Key != ConsoleKey.Enter);

            Console.WriteLine();

            return pass;
        }
    }
}

4 Comments

  1. Stefan Bauer Says

    Nice sample but from time to time you have the script to run again. Hope that some day a provisioning handler for lists and libraryies exists too. 😉

  2. Toni Frankola Says

    Yeah, not ideal. A centralized governance policies would be much better solution.

  3. Jose Says

    Please note that the claims helper class will only work for https:// url. If you are trying to authenticate to your public website site collection you will get authentication errors.

  4. Toni Frankola Says

    @Jose: Never tried that. However even public site should have an alternative URL like https://company-public.sharepoint.com

Leave a Reply