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

User controls are a 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 a 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: let’s display the currently logged-in user name in the label. Get into the code behind the 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
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 a user control to the 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. A similar method applies to add a 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!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

9 thoughts on “How to Create User Control in SharePoint – Step-by-Step Walk through

  • 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

    Reply
  • 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.

    Reply
    • Figured and got it working. Had to add the Tag registration under the BODY tag and not in top section

      Reply
  • 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?

    Reply
  • Wonderful thank you so much…

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *