Ads

Friday, 15 July 2016

Create & add user control in master page

Lets create a user control which will display the login name of user.

Below steps needs to be performed.

Step-1:
VS --> empty Sharepoint Project --> Add user control (UControl)
Add below code in aspx page
<div id="Main" style="border:1; background-color:#2C84CA; font-size: 25px; color:White;">
    <asp:Label ID="lblUserName" runat="server" Text="0"></asp:Label>
</div>


In code behind file
 

Build and deploy the project. This will create new folder in 14 hive's Controltemplate's folder (In my case it was: \14\TEMPLATE\CONTROLTEMPLATES\MyUserControl\MyUserControl.ascx)


SharePoint 2010 add user control to page

1. Open SharePoint designer, Open the target site, and then Default.aspx in advanced mode.
2. Register the user control at the top of the page.
<%@ Register TagPrefix="MyUserControl"  TagName="UserName" Src="~/_controltemplates/MyUserControl/MyUserControl.ascx" %>

Insert the user control wherever required.

We've created user control for SharePoint 2010, Registered it on the page. Now, the final step is to  include the user control in the page, Here I've coded like:
 <MyUserControl:UserName id="MyUserControl1" runat="server" />



Result:

Refer Here





Thursday, 14 July 2016

Add web part zone in master page to add webpart


In master page, there is a content place holder:  
<asp:ContentPlaceHolder id="PlaceHolderLeftActions " runat="server">
</asp:ContentPlaceHolder>


So if you place the SAME ID for my web part zone in the Page layout/Page, at run time, The Master Page's content on PlaceHolderLeftActions will be replaced by the content page

In simple steps:
1. Create a Blank .Aspx page with SPD
2. Just insert the code as per the below screenshot

<asp:ContentPlaceHolder id="PlaceHolderLeftActions " runat="server"></asp:ContentPlaceHolder>



Note:
Same way, If you want to insert a webpart in Master page, you can use the same Technique, insert your own Tag in Master page and replace the content in your content page by assigning same tag.
<asp:ContentPlaceHolder id="MyTag" runat="server"></asp:ContentPlaceHolder>



Ads