SIGN UP MEMBER LOGIN:    
ARTICLE

Changing Welcome Page of SharePoint site Programmatically

Posted by Bhushan Gawale Articles | SharePoint February 09, 2011
Some sample code for programmatically changing the Welcome Page of a SharePoint site
Reader Level:


While working with publishing sites, I came across a requirement to change the welcome page of the site.

Well the first thing which comes to mind is to use a server object model and make use of the Publishing APIs and yes that's good; now a second thought woiuld be a general plan such as getting a Publishing Web object and setting the Default Page url, but note that the Default Page property of the Publishing Web is read only.

So I tried some googling and came across some results and tried one and modified the code.

I utilized our buddy the reflector to see how Microsoft has done this in some cases.

Here is the sample code I wrote and that works fine for me.

namespace ChangeWelcomePage
{
 class Program
 {
  static void Main(string[] args)
  {
   try
   {
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     using (SPSite site = new SPSite("http://siteName:port"))
     {
      using (SPWeb web = site.OpenWeb())
      {
       web.AllowUnsafeUpdates = true;
       if (PublishingWeb.IsPublishingWeb(web))
       {
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

        if (publishingWeb != null)
        {
         SPFolder rootFolder = web.RootFolder;
         SPFolder pagesFolder = publishingWeb.PagesList.RootFolder;
         try
         {
         //Ensuring Root Folder
          if (rootFolder != null)
          {
            //Ensuring Pages Root Folder
            if (pagesFolder != null)
            {
              string newWelcomePageUrl = publishingWeb.PagesList.Title + "/" + "YourPage.aspx";
              rootFolder.WelcomePage = newWelcomePageUrl;
                                               
              if (newWelcomePageUrl.StartsWith(pagesFolder.Url, StringComparison.OrdinalIgnoreCase))
              {
                pagesFolder.WelcomePage = newWelcomePageUrl.Substring(publishingWeb.PagesList.RootFolder.Url.Length + 1);
                                                    pagesFolder.Update();
              }

                 rootFolder.Update();
                 web.Update();
                 publishingWeb.Update();
                 Console.WriteLine("done");
             }
            }
        }
     catch (Exception ex)
     {
       Console.WriteLine(ex.Message);
     }
     finally
     {
       if (publishingWeb != null)
       {
         publishingWeb.Close();
       }
     }
    }

   }
    web.AllowUnsafeUpdates = false;
   }
   }
  });
   Console.ReadLine();
 }
 catch (Exception ex)
 {
     //handle exception
 }
 }
 
}
}
 

Login to add your contents and source code to this article
share this article :
post comment
 

Simple and easy article, images will make it more easy.

Posted by Sapna Feb 09, 2011

Good Example Bhushan.

Posted by Dinesh Beniwal Feb 09, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor