SharePoint 2010: Add User Control to Master Page Using SharePoint Designer 2010 - SharePoint Development & Administration + InfoPath

Tuesday, October 11, 2011

SharePoint 2010: Add User Control to Master Page Using SharePoint Designer 2010

In this article I explain the steps to include a custom User Control in a Master Page in SharePoint 2010 using SharePoint Designer.  

In my case, the example User Control I am adding to the Master Page in this tutorial is in the following location which would vary depending on the name and of the Control and the folder it is saved in:  “~/_controltemplates/SP2010_UserControl_Example/DynamicSiteTitle.ascx file”

SharePoint Designer 2010 Master Page User Control
Using SharePoint Designer 2010, open the Site and Master Page Library where the Master Page will be stored containing the User Control.  In this example, I am creating a copy of the default v4.master used in SharePoint 2010 in the root site Master Page Library and calling it v4b.master.

Open the Master Page you wish to modify by right clicking the and selecting the “Edit File in Advanced Mode” option.

Close to the top of the markup below the existing @Register / @Import tags, add the following line to register the User Control for use in the page.  Change the reference to the User Control file (.ascx) to point to your User Control if required:

<%@ Register TagPrefix="custom" TagName="DynamicTitle" src="~/_controltemplates/SP2010_UserControl_Example/DynamicSiteTitle.ascx" %>

Now that the User Control is registered, it can be included in the page by adding an element with the tag prefix and tag name specified when registering the control.  As the example User Control displays a dynamic title the tag and prefix have been named to suit, but can be something else if required.  The element in this case will look like:

<custom:DynamicTitle id="DynamicTitle1" runat="server"/>

The value of the id attribute can also vary, which should be unique for each instance that the control is included on the page if more than once.  

I have added the control to the Site Name placeholder (PlaceHolderSiteName) replacing the existing Property used to display only the title of the current site, as the custom control performs a similar function.

The resulting code looks like:

<h1 name="onetidProjectPropertyTitle">
<asp:ContentPlaceHolder id="PlaceHolderSiteName" runat="server">
<custom:DynamicTitle id="DynamicTitle1" runat="server"/>

Save the Master Page, and enable it if required on a SharePoint Site.

The Custom Control should be displayed where you placed it on the Master Page.  In this case, the Site title (Team Site), List/Library (Site Pages) and page title (Home) are displayed in the banner of the site.

For assistance with creating a User Control, please see the following tutorial which contains instruction on how to create the example User Control used in this article that displays a dynamic title of the current site, list and page / list item if applicable for demonstration purposes:

Developing a custom User Control for SharePoint 2010

The tutorial covers building a basic User Control from scratch using Visual Studio, and also includes instructions to include the control on an Application page or Master Page.  A User Control can be add directly to any .aspx page in SharePoint as well by following the same steps described in this tutorial.

It is also possible to complete these steps in a Master Page included in a solution developed using Visual Studio 2010, which can then be activated on specific sites as a feature, but is not covered in this article.  See this article by Mirjam: Deploying a custom master page in SharePoint 2010 for instructions on how to add a Master Page to a Feature using Visual Studio, which also include an event receiver to use the new Master Page when the Feature is activated on a SharePoint site.

Share this article:
Stumble This Delicious
submit to reddit

No comments:

Post a Comment