Weird bug while updating WelcomePage using C#

weird-bug-while-updating-welcomepage-using-c

You can change the WelcomePage of your site by updating the value of SPFolder.WelcomePage. However, you might experience some strange behavior if your are trying to update this value directly.

The welcome page of your site won’t be updated if your set the value like this:

SPContext.Current.Site.RootWeb.RootFolder.WelcomePage = newUrl
SPContext.Current.Site.RootWeb.RootFolder.Update();

The correct way to update the welcome page seems to be:

SPFolder webRootFolder = SPContext.Current.Site.RootWeb.RootFolder;
webRootFolder.WelcomePage = newUrl
webRootFolder.Update();

The difference? You have to store the SPFolder object in a separate variable before updating the WelcomePage property, otherwise the welcome page of your site won’t be updated.

I have written a small WebPart so you can try it yourself:
ChangeHomePageWebPart
Add this code to a WebPart:

namespace AlainDeKlerk.SP2010.SetHomePage.ChangeHomePageWebPart
{
    [ToolboxItemAttribute(false)]
    public class ChangeHomePageWebPart : WebPart
    {
        private RadioButtonList pageList;

        protected override void CreateChildControls()
        {
            pageList = new RadioButtonList();
            pageList.DataSource = SPContext.Current.Web.Lists["Pages"].Items;
            pageList.DataTextField = "Title";
            pageList.DataValueField = "Url";
            pageList.DataBind();
            this.Controls.Add(pageList);

            LinkButton buggyMethodLink = new LinkButton();
            buggyMethodLink.Text = "Update (buggy)";
            buggyMethodLink.Click += new EventHandler(buggyMethodLink_Click);
            buggyMethodLink.Style.Add("display", "block");
            this.Controls.Add(buggyMethodLink);

            LinkButton correctMethodLink = new LinkButton();
            correctMethodLink.Text = "Update (correct)";
            correctMethodLink.Click += new EventHandler(correctMethodLink_Click);
            buggyMethodLink.Style.Add("display", "block");
            this.Controls.Add(correctMethodLink);
        }

        void correctMethodLink_Click(object sender, EventArgs e)
        {
            SPFolder webRootFolder = SPContext.Current.Site.RootWeb.RootFolder;
            webRootFolder.WelcomePage = pageList.SelectedValue;
            webRootFolder.Update();

            this.Controls.Add(new LiteralControl(String.Format("WelcomePage set to {0}", SPContext.Current.Site.RootWeb.RootFolder.WelcomePage)));
        }

        void buggyMethodLink_Click(object sender, EventArgs e)
        {
            SPContext.Current.Site.RootWeb.RootFolder.WelcomePage = pageList.SelectedValue;
            SPContext.Current.Site.RootWeb.RootFolder.Update();

            this.Controls.Add(new LiteralControl(String.Format("WelcomePage set to {0}", SPContext.Current.Site.RootWeb.RootFolder.WelcomePage)));
        }
    }
}

About the author

Alain

You can leave a response, or trackback from your own site.

One Response to “Weird bug while updating WelcomePage using C#”

  1. Andez says:

    Thanks for that. Very strange – even in SP2010. Might have been sitting here for hours scratching my head!

Leave a Reply