Master page and page layout reporting

A few weeks ago while working on a publically facing website built in MOSS 2007 I had to make some changes that affected all page layouts and master pages. The problem was overtime lots of test and unused page layouts and master pages had been left in the master page gallery. Rather than add the changes to all the page layouts and master pages I decided to write a tool that iterated through all webs and all pages in a site collection outputting the master page and page layout being used.

Since I am used to writing them I created a windows forms application which asks the user to supply the URL of the site, the only downside to this is the tool must be run on one of the web front end (WFE) servers.For my purposes I decided to take a snapshot of the site at a specific point and essentially export the data to a SQL DB. I create a variety of tables to store the information but I won’t cover this here as what I needed may not be required by most people. The reason I took this approach is I wanted to do some reporting on the data and didn’t want to be constantly programmatically accessing the site in case it had an effect on the performance.

Since I am not covering the DB side of things i have adjusted the code below to instead output all the details to a text file, this should give you an idea of what the process would be and you can alter it as you see fit.

Please note that while I have tested and ran this several times all use is at your own risk and strongly recommend this is tested in a development environment prior to being used on a production site.

private void button1_Click(object sender, EventArgs e)

{

    if (String.IsNullOrEmpty(TbxSiteURL.Text))

    {

        MessageBox.Show("Please enter a URL");

        return;

    }

 

    //open site and get web

    using (SPSite site = new SPSite(TbxSiteURL.Text))

    using (SPWeb web = site.OpenWeb())

    {

        //call function to loop through all sites and publishing pages

        IterateWebs(web);   

    }

}

 

/// <summary>

/// An iterative function that takes a site and log details to a text file such as masterpage and 

/// publishing pages page layouts. It then loops through all subsites of the site

/// passed in and calls the method again.

/// </summary>

/// <param name="web">A SPWeb object of the site to log details and check subsites</param>

private void IterateWebs(SPWeb web)

{

    string message = String.Format("Web: {0}{1}Location: {2}{1}Masterpage URL: {3}{1}",

        web.Title, Environment.NewLine, web.Url, web.MasterUrl);

    

    LogWebDetails(message);

 

    if (PublishingWeb.IsPublishingWeb(web))

    {

        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

 

        foreach (PublishingPage page in pubWeb.GetPublishingPages())

        {

            string pageLayout = page.ListItem.Properties["PublishingPageLayout"] as string;

            message = String.Format("Page name: {0}{1}Page URL: {2}{1}Page layout: {3}{1}",

                        page.Title, Environment.NewLine, page.Url, pageLayout);

            LogWebDetails(message);

        }

    }

    else

    {

        LogWebDetails("Web is not a publishing web" + Environment.NewLine);

    }

 

    foreach (SPWeb childWeb in web.Webs)

    {

        try

        {

            IterateWebs(childWeb);

        }

        finally

        {

            if (childWeb != null)

            {

                childWeb.Close();

            }

        }

    }            

}

 

/// <summary>

/// Log details to a text file

/// </summary>

/// <param name="logMessage">A string of the message to be written to the file</param>

private void LogWebDetails(string logMessage)

{

    File.AppendAllText("Log.txt", logMessage);

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: