Ethan Deng's SharePoint Blog

About Me
SharePoint
Blog
List Pagination 1
List Pagination 2
Customize Home Page
Excel Services Issue
Error with mngfield Page
User Defined Function
Enable Incoming Email
Install SP2 On R2
SharePoint Content Migrat
SharePoint2010 Installati
Troubleshoot Search
Dash Sign And Search
Search Pattern Issue
Create Dropdown Menu
Email Enabled Custom List
Email Enabled Issue List
SharePoint Folder
Cost Effective Small Farm
SharePoint Throttling
SystemUpdate And Alerts
A Parallel Approach

Replace and Customize SharePoint Default.aspx Home Page

At the first thought, I thought this should be an easy task. However after a few tries, I found it is not that easy.

Usually to provision a SharePoint site page, you use Module in feature activation as

  <Module Name="Default" Url="" Path="SitePages">

    <File Url="default.aspx" NavBarHome="True" Type="Ghostable" />

  </Module>

 

So to replace the default.aspx home page, it should be as easy as this. However, it doesn’t work since for any existing site pages, the module would work. You can use module to create new site pages but not to overwrite the existing site pages.

After a few searches, I understand I have to delete the existing page first. Of course you can do this from SharePoint Designer but as a solution development project this can be done in FeatureActivated event handler in FeatureReciever class.

So I write the code to delete the file, however after I activated the feature, the default.aspx was gone. I think what happened is that the “Module” happened before the FeatureActivated event and the FeatureActivated event handler just delete the default.aspx provisioned by Module section.

Now it is clear that you can’t use “Module” to overwrite existing site page. You have to use Object Model in FeatureActivate event handler to manually delete and created the file.

The final code that works look like this

   public class FeatureReceiver: SPFeatureReceiver

    {

 

        SPWeb web = null;

        public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            try

            {

                // properties.Feature is null in FeatureInstalled.

                using (SPSite site = (SPSite)properties.Feature.Parent)

                {

                    web = site.RootWeb;

                    ReplaceHomePage(properties);

                    AddNavigationNodes();

                }

            }

            catch (Exception expt)

            {

                string error = expt.Message;

                System.Diagnostics.Debugger.Launch();

            }

        }

       private void ReplaceHomePage(SPFeatureReceiverProperties properties)

        {

            // Delete the default.aspx

            SPFile defaultAspx = web.GetFile("default.aspx");

            defaultAspx.Delete();

 

            // Use Object Model to add default.aspx page instead of using Module in feature

            SPFolder rootFolder = web.RootFolder;

            rootFolder.Files.Add("default.aspx", File.OpenRead(properties.Feature.Definition.RootDirectory + @"\SitePages\default.aspx"));

        }

Another thing I learned is that you can use

            System.Diagnostics.Debugger.Launch();

 

To debug feature activation since feature activation is run by STSADM on command line, it is hard to attach the debugger to this process.