Setting automatic page title for SharePoint Default pages

Here is the scenario: I have to create several doesens of subsites under my site collection. Each of the subsites will have their Default.aspx page with predefined set of webparts and other things set up. Among those other things – Default  page will inherit it’s look and feel from its Layout. To make my page inherit from the layout I have created a SharePoint Feature that I can easily use to automate my subsite creation in my deployment script.

The problem: Since I’m creating all of the sites automatically through my feature called in my deployment script, I have no way of assigning appropriate page titles to the Default  page.

The solution: One of the easiest approaches is to create a feature that will set default page title to the title of the site upon activation. That feature we can call within out deployment script right after the site creation feature.

Since our SharePoint page title feature will execute events upon its activation we’ll create a class that will be our event receiver executing all of the logic. Here is how out Feature.XML will look like:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/
 Id=”B84C3CF3-1346-4830-B39B-00C9F6EBCA28″
 Title=”Set Page Title”
 Description=”Setting page title.”
 Version=”1.0.0.0″
 Scope=”Web”
 Hidden=”TRUE”
 ActivateOnDefault=”TRUE”
 AlwaysForceInstall=”TRUE”
  ReceiverAssembly=”MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1ccd23ff14aa10aa”
 ReceiverClass=”MyProject.TitleFeatureReceiver”>
</Feature>

In here you can see that we’re referring to a feature receiver called TitleFeatureReceiver. We’ll create a new class and call it TitleFeatureReceiver, our class will inherit from SPFeatureReceiver.

Within our feature here is how out FeatureActivated method looks like:

using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
// Assuming we activate this feature only on publishing pages
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
SPFile defaultPage = pubWeb.DefaultPage;

defaultPage.CheckOut();
defaultPage.Update();

defaultPage.Item["Title"] = web.Title;
defaultPage.Item.Update();

defaultPage.CheckIn(“Title automatically set”);
defaultPage.Update();
}

That’s all !

This entry was posted in MOSS, sharepoint and tagged , , , . Bookmark the permalink.

Comments are closed.