Thursday 27 October 2011

The All-Singing, All-Dancing Site Collection Recursion Code!

Need to update some setting in every site in your site collection? Hunting around the internet googling "Recursion", "Site Collection" and "SharePoint"? Here I am to your rescue. Create a console application, add in a ref to Sharepoint and replace the one line in Main() with your site collection. Stick your own code into the bit where it says "Put your code here", run, and Bob's your uncle, auntie and whatever sort of relative you're having yourself. Can be adapted for web part too if you want. The streamwriter dumps a load of updates into your chosen folder.

Why, you're welcome :)

//Code by Susan Lanigan, 25/10/2011

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



namespace RecursiveConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

            //call once and let recursion take care of the rest
          //start from a site collection top level URL
            //NOTE - if you want to break completely on an error
            //stick a try-catch block up here and get the sub to throw;
            DoTheWork("http://myServer/mySiteColl", string.Empty);


        }

        static void DoTheWork(string siteUrl, string parentWebName, string eMail)
        {
            string errorArgs = string.Empty;

            try
            {

                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        // another try-catch for the filestreamer
                        //write console output to file
                        FileStream fs = new FileStream("c:\\Susan\\LogForSite_" + parentWebName + "_" + web.Name + ".txt", FileMode.Create);
                        StreamWriter sw = new StreamWriter(fs);
                        TextWriter tmp = Console.Out;

                        try
                        {

                            Console.SetOut(sw);
                            Console.WriteLine("Writing update for site: " + web.Url);
                            //DO YOUR WORK HERE AND WRITE TO STREAMWRITER ETC.

                            //...


                        }
                        catch (Exception fileEx)
                        {
                            Console.WriteLine(fileEx.Message);
                            return;
                        }
                        finally
                        {
                            sw.Close();
                            fs.Close();
                        }

                        //reset console output
                        Console.SetOut(tmp);

                        //now for the subsites
                        SPWebCollection subSites = web.Webs;

                        foreach (SPWeb subSite in subSites)
                        {
                            //RECURSION HERE
                            DoTheWork(subSite.Url, web.Name, eMail);
                            subSite.Close();
                        }

                    }



                }

            }



            catch (Exception ex)
            {
                //custom error handling stuff
                Console.WriteLine(ex.Message);
                Console.ReadLine();
               
                return;
            }

        }
        }

}

No comments:

Post a Comment