Wednesday, February 10, 2010

How to deploy Custom Masterpage with Codebehind

How to deploy Custom Masterpage with Codebehind

Custom Masterpage with custom Codebehind is strong tool for developing sites with extra functionality.

Codebehind could be attached to Masterpage also at runtime, if we update Masterpage in Sharepoint Designer and we have deployed dll with Codebehind in Global Assembly Cache (GAC).

The easier way is to attach Codebehind to Custom Masterpage at development time in Visual Studio.

In this article i will provide tutorial in which i describe how to deploy Custom Masterpage with Codebehind within WSP Solution package.

You should follow these steps:
  1. Create a Blank Site Definition
  2. Attach Custom Masterpage to solution
  3. Update Site definition
  4. Add Custom Codebehind to project
  5. Attach Codebehind to Masterpage
  6. Deploy solution and create Site collection

1) Create a Blank Site Definition

- in Menu choose: File -> New -> Project
- in tree list navigate to Sharepoint group and choose Blank Site Definition
- fill the Name and Location of project and click OK

creating blank site definition

2) Attach Custom Masterpage to solution

- locate file default.master in folder: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\GLOBAL\default.master
- right click the file and choose Copy
- go to Visual Studio Solution explorer
- right click the folder Site definition under the created Project
- choose Paste from context menu
- right click the inserted file default.master and choose Rename
- set the name of file to: demosite.master
- in Masterpage add ASP Control as shown on picture:

masterpage asp control
- XML code of ASP Control



3) Update Site definition

- locate and open for update onet.xml file in Solution explorer
- navigate to Configuration node with ID=1 under Configurations node
- add attributes CustomMasterUrl and MasterUrl as shown on picture

updating site definition
- navigate to Modules node under the edited Configuration node
- create new reference Module node under Modules node as shown on picture
- navigate to Modules node under Project node
- create new definition Module node as shown on picture

updating site definition
- code for copy and paste for onet.xml file:













4) Add Custom Codebehind to project

- right click the folder Site definition in the Solution explorer and choose Add new item
- choose Code file and name it demosite.master.cs
- when you click the Properties of file, you should see Build Action: Compile
- the minimal class code is shown on the picture
- notice that the class must derive from System.Web.UI.MasterPage class
- the namespace of class have to be the same as in the SiteProvisioning.cs file
- in this class we have reference to asp:Label control which we have added to Masterpage template

custom codebehind
- code for copy and paste:

namespace BlankSiteDefinitionDemo
{
public class MasterPageCodeBehind : System.Web.UI.MasterPage
{
protected global::System.Web.UI.WebControls.Label CustomLabel;

protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
CustomLabel.Text = "";
}
}
}

5) Attach Codebehind to Masterpage

- in file demosite.master you have to add reference to Codebehind
- add attributes CodeBehind and Inherits to the first line of master page template as shown on picture
- attribute CodeBehind contains file name and relative path of Codebehind file
- attribute Inherits contains the reference to dll, first subparameter is namespace plus dot plus classname, second subparameter is namespace again, third subparameter is token of our DLL Library
- the token of our DLL Library could be obtained in an easy way: first quick deploy project and choose copy binaries (Project is built and DLL are copied to GAC - Global Assembly Cache), open My computer and go to location: c:\windows\assembly, libraries are sorted by name, locate namespace of our Project, right click on it and choose Properties, then just copy the token
- Masterpage should look like this:

masterpage with attached codebind
- XML code for copy:

< %@Master language="C#" CodeBehind="demosite.master.cs"
Inherits="BlankSiteDefinitionDemo.MasterPageCodeBehind,
BlankSiteDefinitionDemo, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=3728f88d7c774d6a" %>

6) Deploy solution and create Site collection

- when you have attached customized Masterpage to WSP Solution package, next step are common for most deployment scenarious, just deploy solution and create site collection
- after you done it, site should be displayed with custom master page and label should be updated:

updated masterpage
Custom Masterpage was deployed successfuly with solution WSP package and the ASP control on it was correctly accessed from codebehind.

Back to Top