Monday, April 9, 2012

How to Create User Control in SharePoint 2010 - Step by Step Walk through

User controls are rapid way to develop custom functionality. There are three steps involved in creating user controls. This article walks through step by step on creating user controls in SharePoint 2010 which displays currently logged in user.

  1. Create and deploy user control
  2. Register user control in the target
  3. Insert the user control wherever required.
Step 1: Create and deploy user control
1. Fire up visual studio 2010, Create a new Empty SharePoint Project.

2. Add an user control to the Project, say "MyUserControl", Project structure will look like this:
sharepoint 2010 create user control


3. Lets build the control now. Go to the code view, Insert a <DIV> and label. Set some CSS styles as like this:
<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>

4. The idea is: Lets display the currently logged in user name in the label. Get in to the code behind file (.cs) add this little code.
        
namespace MyUserControl.ControlTemplates.MyUserControl
{
    public partial class MyUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string currentUser = "Current User Name:" + SPContext.Current.Web.CurrentUser.Name;
            lblUserName.Text = currentUser;
        }
    }
}
SharePoint 2010 user control code behind 
sharepoint 2010 user control code behind
5. Deploying user control in SharePoint 2010: 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)

Step 2: Register user control in the target

We have our user control ready now. Next step is insert the user control wherever needed. Here, Lets add it to a SharePoint page. say, Default.aspx. By the way, You can load the user control wherever you want, like in Pages, Master pages, Page layouts, and even in Web parts (use Page.LoadControl method)

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" %>

sharepoint 2010 add user control master page 

Step 3: 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" />
sharepoint 2010 deploy user control
 That's all! See it in action: SharePoint 2010 user control example. Similar method applies to add user control to SharePoint 2010 master page.
sharepoint 2010 user control example

You don't need to add the user control assemblies in <SafeControl> entry under web.config file explicitly, as all the controls in side CONTROLTEMPLATES folder of 14 hive are trusted by default!



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

9 comments :

  1. Wonderful thank you so much...

    ReplyDelete
  2. I want to create user control in a client side.
    i want to create People picker Control and that should run in client side
    so that there will be no .cs file and no execution at server side.
    How to do that?

    ReplyDelete
  3. Great article. I tried the same. Was able to place the user control on page layout and see it working fine. But when I placed the registration and user control tag on the master page layout, it gives error
    spcuc:MyUserControl not known.... where spcuc is my tagprefix and MyUserControl is the tagname.

    ReplyDelete
    Replies
    1. Figured and got it working. Had to add the Tag registration under the BODY tag and not in top section

      Delete
  4. In same way I want to show Custom Breadcrumb of another site collection .
    Example: Sitecollection(A)'s URl should show as a custom breadcrumb in another site collection(B).
    please suggest me to achieve it

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...