Sunday, 27 April 2014

How to add user control to Sharepoint master page.

How to add user control to Sharepoint master page.

we can add user control using two ways
a] Register user control into master page
b] using module

Consider-
Project Name: MasterPageControl
master page:  v4.master
User control: RestrictPasteUrlb.ascx

Firstly add user control in your project.
   right click on project -> Add new item->select User Control
    (write your code in that)

using a first way i.e. Register user control into master page

1] Register user control in master page.

<%@ Register  TagPrefix="myControl"  TagName="CurrentPageControl"
Src="~/_controltemplates/MasterPageControl/ RestrictPasteUrlb.ascx"  %>

Src : is a path where user control is placed. i.e  in Template folder -> controltemplates  -> Folder of project name in our case (MasterPageControl)

2] Then you can add control to master page.

<mycontrol:CurrentPageControl Id="myControl" runat="server"/>

3] Delopy

using second way i.e Using Module.

1] Add module into project
right click on project -> Add new item->select Module.

2]Open Element.xml
(Here we will register our user control using module)

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Module">
    <File Path="Module\Sample.txt" Url="Module/Sample.txt" />
  </Module>
      <Control ControlSrc="~/_controltemplates/ MasterPageControl /RestrictPasteUrl.ascx" Id="AdditionalPageHead" />
</Elements>

ControlSrc : It's a path where user control is placed. i.e  in Template folder -> controltemplates  -> Folder of  project name in our case (MasterPageControl)
Id : It's a control id of sharepoint delegate control where we are going to add our user control  on master page (v4.master).

3] Delopy

I would preferred second way (using module ).








No comments:

Post a Comment