Make Custom Landing Page As Default Page of the SharePoint Team Site same as Wiki Home Page

Have you ever looked at how Microsoft has implemented SharePoint 2010 team site default home page as wiki home page? Whenever end-user provisions new site based on team site template, Microsoft not only creates the new team site but activates the web level feature called “Wiki Page Home Page”, which would provision two new document libraries called “Site Pages” and “Site Pages”, deploys the new home page called “Home.aspx”, and sets the “/SitePages/Home.aspx” as a default home page.

What if you ever wanted to use same pattern? Say you have requirements where you would need to provision new sites and site landing pages, and automatically redirect users to new landing page as a home page. To implement similar pattern, follow following steps.

Step 1 = Create New visual Studio Empty SharePoint Solution for Custom Site Landing Page – CustomHomePage and create New Custom Site Landing Page as a site level page using Module element – Home/Home.aspx.

Step 2 = Implement feature receiver for the feature deploying custom landing page. Add following code in both feature activated and feature deactivating events. During activation process, you can use WelcomePage property of the SPFolder object to set new page as home page. During deactivation process, you can rollback and set the default.aspx as landing page.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPWeb spWeb = (properties.Feature.Parent as SPWeb))
    {
        //Make custom landing Page as Site Home Page             
        SPFolder rootFolder = spWeb.RootFolder;
        rootFolder.WelcomePage = "Home/Home.aspx"; //custom landing page    
        rootFolder.Update();
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPWeb spWeb = (properties.Feature.Parent as SPWeb))
    {
        //Make default site page as Site Home Page
        SPFolder rootFolder = spWeb.RootFolder;
        rootFolder.WelcomePage = "default.aspx"; //standard team site default page
        rootFolder.Update();
    }
}

Step 3 – Deploy the CustomHomePage solution and activate the Custom Home Page feature. This feature would deploy the Home/Home.aspx page to the site and set custom page as a default page.

Step 4 – Accessing root URL of the site where custom solution deployed should take you to the Home/Home.aspx as landing page. It’s that easy.

Additional References

 – http://stackoverflow.com/questions/1649833/replace-the-default-page-in-a-sharepoint-website
 – http://attis.org/blogs/dan/archive/2008/10/29/how-to-change-the-default-page-of-a-sharepoint-site-using-a-feature.aspx

Advertisements
This entry was posted in SP2010 DEV General. Bookmark the permalink.