Welcome Page Updater

During a recent uprade from Sharepoint 2003 to 2007, I noticed that some of the sites’ welcome pages weren’t getting set correctly. For some reason, they were set to UpgLandingPgRedir.aspx, which redirected to nowhere. Very convenient; must be a feature.

Here’s a piece of code I wrote that looks at every weclome page in your site collection and changes it if necessary. The Pause() isn’t necessary; I found it helpful to verify the changes that were being made.

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

namespace SPWelcomePageUpdater
{
    class SPWelcomePageUpdater
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter the URL of the Sharepoint site: ");
            string url = Console.ReadLine();
            using (SPSite siteCollection = new SPSite(url))
            {
                SPWebCollection coll = siteCollection.AllWebs;

                for (int i = 0; i < coll.Count; i++)
                {
                    using (SPWeb targetWeb = coll[i])
                    {
                        SPList pagesLib = targetWeb.Lists["Pages"];
                        SPFile newWelcomePage = null;
                        foreach (SPListItem li in pagesLib.Items)
                        {
                            if (li.Name == "default.aspx")
                            {
                                newWelcomePage = li.File;
                            }
                        }

                        if (newWelcomePage != null)
                        {
                            if (PublishingWeb.IsPublishingWeb(targetWeb))
                            {
                                PublishingWeb publishingWeb = 
                                     PublishingWeb.GetPublishingWeb(targetWeb);
                                Console.WriteLine(publishingWeb.DefaultPage.Name);
                                if (publishingWeb.DefaultPage.Name=="UpgLandingPgRedir.aspx")
                                {
                                    // this sets the new welcome page
                                    publishingWeb.DefaultPage = newWelcomePage;    
                                    publishingWeb.Update();
                                    Console.WriteLine(publishingWeb.Name 
                                         + " welcome page updated.");
                                    Pause();
                                }
                                else if (!publishingWeb.DefaultPage.Url.ToUpper()
                                                .Contains("PAGES"))
                                {
                                    // this sets the new welcome page
                                    publishingWeb.DefaultPage = newWelcomePage;    
                                    publishingWeb.Update();
                                    Console.WriteLine(publishingWeb.Name 
                                         + " welcome page re-updated.");
                                    Pause();
                                }
                            }
                        }
                    }
                }
            }
        }
        static void Pause()
        {
            Console.WriteLine("Press any key to continue...");

            Console.ReadKey(true);
        }
    }
}

Comments are closed.

Post Navigation

Switch to our mobile site