Monday, January 31, 2011

Programmatically create custom default page for SharePoint 2010 Team site

In this article, I will explain how to programmatically create custom default page and set it to root level.

Step 1. Select SharePoint empty project template and then add Module.
Step 2.Update the elements manifest to tell where to find the custom file /Page and where it needs to deploy.

. ?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">. ?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">
File Path="Home\default.aspx" Url="default.aspx" IgnoreIfAlreadyExists="TRUE" Type="Ghostable">
/File>
/Module>
/Elements>


Step 3. Add event handler to the feature.
SPWeb oWeb = null;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
oWeb = (SPWeb)properties.Feature.Parent;
SPFolder rootfolder = oWeb.RootFolder;
rootfolder.WelcomePage = "default.aspx";
rootfolder.Update();
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
oWeb = (SPWeb)properties.Feature.Parent;
SPFolder rootfolder = oWeb.RootFolder;
rootfolder.WelcomePage = "SitePages/Home.aspx";
rootfolder.Update();
}

No comments:

Post a Comment