Wednesday, June 29, 2011

Deploy Welcome Page in Team Sites - Method3

Method: 3
We will deploy the new Home Page to the 12th hive and not to the Layouts Folder because I want the look and feel of the URL as http://sites/test/CustomPages/Home.aspx but not like http://sites/test/_Layouts/Home.aspx
The way of doing this is through Feature Receiver when activated sets the new Home Page and deactivated sets to the older Home Page.
Solution Structure

Feature.xml:
The Element Manifest Section should be like this
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
    <ElementFile Location="CustomPages\Home.aspx"/>
  ElementManifests>
Elements.xml:
<Module Name="CustomPages" Path="CustomPages" >
     <File Path="Home.aspx" Url="CustomPages/Home.aspx" Type="Ghostable"/>
  Module>
Here Comes Our FeatureActivated code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            
            SPWeb web = properties.Feature.Parent as SPWeb;    
           // back up the original home page
           SPFile defaultPage = web.Files["default.aspx"];
           defaultPage.MoveTo("default-old.aspx");
           //updating the navigation
           SPFolder rootFolder =web.RootFolder;
           rootFolder.WelcomePage = "CustomPages/Home.aspx";
           rootFolder.Update();
        }
FeatureDeactivating code:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        { 
            //CustomPages/Home.aspx
            SPWeb web = properties.Feature.Parent as SPWeb;
            SPFolder rootFolder = web.RootFolder;           
            // restore the back up
            SPFile originalDefaultPage = web.Files["default-old.aspx"];
            originalDefaultPage.MoveTo("default.aspx");
           //resetting up the navigation 
            rootFolder.WelcomePage = originalDefaultPage.ToString();
            rootFolder.Update();            
        }

No comments:

Post a Comment