Changing properties of default.aspx through feature

We had a request once to change the width of columns on default.aspx on sites in SharePoint. Though we tried to do this in CSS, there just weren’t any ID tags on the columns, so we couldn’t. For SharePoint designer this is easy to do but we wanted to remove that as making changes with SPD in production is not allowed, and shouldn’t be allowed.

First, we created a feature. In the project we created folders 12/TEMPLATE/FEATURES/DEFAULTMOD/

Added Feature.xml and elements.xml

FEATURE.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature
Id=”GUID”
Title=” MAIN SITE Custom Default file”
Description=”This Feature contains a custom default page for the home page”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”FALSE”
DefaultResourceFile=”core”
ReceiverAssembly=”AAMC_IT_DesignerChanges, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32fc1a05b011eb37″
ReceiverClass=”DesignerChanges.Modifications”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”elements.xml” />
<ElementFile Location=”customDefault.aspx” />
</ElementManifests>
</Feature>

Elements.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”Modifications” Url=”” RootWebOnly=”FALSE”>
<File Path=”customDefault.aspx” Url=”customDefault.aspx” IgnoreIfAlreadyExists=”TRUE” />
</Module>
</Elements>

customDefault.aspx was just changing the middle two columns from the microsoft default of  70/30 to 50/50

Next we created the code that did the swapping out. Notice that when you deactive the feature, the file is just deleted, you can change that and get fancy to back it up as well. You could also do something terribly clever and add the webparts between the files being moved around. We didn’t have that much time and this was for a new site, so we just did a straight swap out.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;

namespace DesignerChanges
{
class Modifications : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
// get a reference to the web
SPWeb web = properties.Feature.Parent as SPWeb;

// back up the original home page
SPFile defaultPage = web.Files[“default.aspx”];
defaultPage.MoveTo(“default-old.aspx”);

// add components to the new custom default page here, if necessary
// move the new default page to default.aspx
SPFile newDefaultPage = web.Files[“customDefault.aspx”];
newDefaultPage.MoveTo(“default.aspx”);

// update navigation, if necessary, here
}

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
SPWeb web = properties.Feature.Parent as SPWeb;

// get a reference to the web

// delete the default page
SPFile defaultPage = web.Files[“default.aspx”];
defaultPage.DeleteAllPersonalizationsAllUsers();
defaultPage.Delete();

// restore the back up
SPFile originalDefaultPage = web.Files[“default-old.aspx”];
originalDefaultPage.MoveTo(“default.aspx”);

}

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
}

Comments are closed.