dinsdag 29 januari 2008

generic feature to install a custom masterpage

During my current job i needed to create a few new visual designs and install them as a masterpage. I searched the internet and found a custom masterpage changer, and used it. But it didnt fullfill my needs. WHen using this solution, i needed to have 7 assemblies for 7 masterpages. I changed the code and the feature.xml a bit to make it more generic. The code isn't too neat, it's more of quickly hacked ;)

How to use it?

Create a new dir with the name of your feature. Put an elements.xml, feature.xml and a dir with the name of your masterpage([name]). put the NewDesign.master, and custom css files (and maybe some other files) into that directory.

FeatureName
|- [Name]
|- Feature.xml
|- Elements.xml

put the ChangeMaster assembly into the GAC.


feature.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="90969656-A1C9-4f40-A95F-A3BDDF5723BF"
Title="Use Custom Master File"
Scope="Web"
ReceiverAssembly="CustomMaster, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=b2f164534d725b81"
ReceiverClass ="CustomMaster.ChangeMaster"
>
<ElementManifests>
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
<Properties>
<Property Key="VisualDesignName" Value="[Name]" />
</Properties>
</Feature>



elements.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="AddMasters" Url="_catalogs/MasterPage" >
<File Url="[NAME]/NewDesign.master" Type="GhostableInLibrary"
IgnoreIfAlreadyExists="True"/>
<File Url="[NAME]/custom.master" Type="GhostableInLibrary"
IgnoreIfAlreadyExists="True"/>
<File Url="[NAME]/custom.css" Type="GhostableInLibrary"
IgnoreIfAlreadyExists="True"/>
<File Url="[NAME]/custom_tables.css" Type="GhostableInLibrary"
IgnoreIfAlreadyExists="True"/>
</Module>
</Elements>


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

namespace CustomMaster
{
public class ChangeMaster : Microsoft.SharePoint.SPFeatureReceiver
{
public override void FeatureInstalled
(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling
(SPFeatureReceiverProperties properties)
{

}
public override void FeatureActivated
(SPFeatureReceiverProperties properties)
{
string strKey = "VisualDesignName";
string name = properties.Definition.Properties[strKey].Value;

SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
string filler = "/";
if (CurrentWeb.ServerRelativeUrl == "/")
{
filler = "";
}
CurrentWeb.MasterUrl = CurrentWeb.ServerRelativeUrl + filler + "_catalogs/masterpage/" + name + "/NewDesign.master";
CurrentWeb.CustomMasterUrl = CurrentWeb.ServerRelativeUrl + filler + "_catalogs/masterpage/" +name + "/custom.master";
CurrentWeb.Update();
}
public override void FeatureDeactivating
(SPFeatureReceiverProperties properties)
{
SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;

string filler = "/";
if (CurrentWeb.ServerRelativeUrl == "/")
{
filler = "";
}

CurrentWeb.MasterUrl = CurrentWeb.ServerRelativeUrl + filler + "_catalogs/masterpage/default.master";
CurrentWeb.CustomMasterUrl = CurrentWeb.ServerRelativeUrl + filler + "_catalogs/masterpage/default.master";
CurrentWeb.Update();
}
}
}