Changing Welcome Page of SharePoint site Programmatically

n

nWhile working with publishing sites, I came across the point where I wanted to change the welcome page of the site

n

nwell the first thing which comes to our mind is to use server object model and make use of Publishing APIs and yes that’s correct , now second thought we generally plan like to get Publishing Web object and set Default Page url .. But note that Default Page property of Publishing Web is read only

n

nSo I tried some googling and came across some results and tried one and modified some code

n

nI took help from buddy reflector to see how Microsoft has done this in some cases

n

nHere is the sample code which I made and works fine for me

n

nnamespace ChangeWelcomePage

n

n{

n

n class Program

n

n {

n

n  static void Main(string[] args)

n

n  {

n

n           

n

n   try

n

n   {

n

n    SPSecurity.RunWithElevatedPrivileges(delegate()

n

n    {

n

n     using (SPSite site = new SPSite(“http://siteName:port”))

n

n     {

n

n      using (SPWeb web = site.OpenWeb())

n

n      {

n

n       web.AllowUnsafeUpdates = true;

n

n       if (PublishingWeb.IsPublishingWeb(web))

n

n       {

n

n        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

n

n

n

n        if (publishingWeb != null)

n

n        {

n

n         SPFolder rootFolder = web.RootFolder;

n

n         SPFolder pagesFolder = publishingWeb.PagesList.RootFolder;

n

n

n

n         try

n

n         {

n

n         //Ensuring Root Folder

n

n          if (rootFolder != null)

n

n          {

n

n            //Ensuring Pages Root Folder

n

n            if (pagesFolder != null)

n

n            {

n

n              string newWelcomePageUrl = publishingWeb.PagesList.Title + “/” + “YourPage.aspx”;

n

n              rootFolder.WelcomePage = newWelcomePageUrl;

n

n                                               

n

n              if (newWelcomePageUrl.StartsWith(pagesFolder.Url, StringComparison.OrdinalIgnoreCase))

n

n              {

n

n                pagesFolder.WelcomePage = newWelcomePageUrl.Substring(publishingWeb.PagesList.RootFolder.Url.Length + 1);

n

n                                                    pagesFolder.Update();

n

n              }

n

n

n

n                 rootFolder.Update();

n

n                 web.Update();

n

n                 publishingWeb.Update();

n

n                 Console.WriteLine(“done”);

n

n             }

n

n            }

n

n        }

n

n     catch (Exception ex)

n

n     {

n

n       Console.WriteLine(ex.Message);

n

n     }

n

n     finally

n

n     {

n

n       if (publishingWeb != null)

n

n       {

n

n         publishingWeb.Close();

n

n       }

n

n     }

n

n    }

n

n

n

n   }

n

n

n

n    web.AllowUnsafeUpdates = false;

n

n   }

n

n   }

n

n  });

n

n   Console.ReadLine();

n

n }

n

n catch (Exception ex)

n

n {

n

n     //handle exception

n

n }

n

n }

n

n }

n

n}

n

n

n

n

n

Leave a Comment