The first step is to add feature to your project, if you have not already one.
I created an empty SharePoint project for this tutorial.
Ensure, that the feature scope is set to "Site" to apply the master page to all webs in this site collection.
Then add a "Module" to the project. You can give any name:
After the module is created it contains an "elements.xml" and "sample.txt" file. The "sample.txt" file can be renamed to "sample.master".
Your project then should cotain these elements:
Best practice: Open the SharePoint Designer, navigate to the current master file and edit it.
Note: In SharePoint 2010 the default master page is "v4.master" and NOT "default.master"
Copy the contents of "v4.master" in your "sample.master" and do your changes on the code.
Then make sure, that your "elements.xml" of the module looks like this:
<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Module Name="Module1" List="116" Url="_catalogs/masterpage"><File Path="Module1\sample.master" Url="sample.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="FALSE" /></Module></Elements>
You could stop here and this solution will deploy your "sample.master" master page to the master page gallery of your site collection.
This will not apply the master page to the site collection when the feature gets activated though. It will only make it available for selection.
This will not apply the master page to the site collection when the feature gets activated though. It will only make it available for selection.
If you want to apply your "sample.master" file, when the feature gets activated, add an event receiver to your feature via right-click on the feature and selection "Add Event Receiver".
Open "Feature1.EventReceiver.cs" and comment in "FeatureActivated" and "FeatureDeactivating" methods. Then paste the following code in it:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPSite site = (SPSite)properties.Feature.Parent)
{
using (SPWeb web = site.RootWeb)
{
// Create full master url
Uri masterUri = new Uri(web.Url + "/_catalogs/masterpage/sample.master");
// Master page used by all forms and pages on the site that are NOT publishing pages
web.MasterUrl = masterUri.AbsolutePath;
// Master page used by all publishing pages on the site
web.CustomMasterUrl = masterUri.AbsolutePath;
web.Update();
}
}
}
catch (Exception exp)
{
throw exp;
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
using (SPSite site = (SPSite)properties.Feature.Parent)
{
using (SPWeb web = site.RootWeb)
{
// Create full master url
Uri masterUri = new Uri(web.Url + "/_catalogs/masterpage/v4.master");
// Master page used by all forms and pages on the site that are NOT publishing pages
web.MasterUrl = masterUri.AbsolutePath;
// Master page used by all publishing pages on the site
web.CustomMasterUrl = masterUri.AbsolutePath;
web.Update();
}
}
}
catch (Exception exp)
{
throw exp;
}
}
If you activate the feature, the code in the feature receiver will be executed and will apply "sample.master" to the site collection. As you can see we change both the MasterUrl and the CustomMasterUrl. The MasterUrl is used on all pages that are not publishing pages. This means it is used on the pages in the sitepages library and on the pages in the _layouts directory like the settings page for instance. The CustomMasterUrl is only used on pages that are stored in the Pages library. This library is created when the SharePoint Server Publishing Infrastructure features is activated on the site collection and the SharePoint Server Publishing feature is activated on the site.
No comments:
Post a Comment