Design User Control Using Visual Studio 2010
Open VS 2010
Create a new Empty SharePoint Project. Name it: SharePointProject1
Select Farm solution option
RIght click on project, add new item
New window will open, Click on Visual C# from Installaed templates..
Search for User Control from roght top search box
Select User Control
Name it: MyUserControl.ascx
It will add new folders: ControlTemplates->SharePointProject1
Inside that "MyUserControl.ascx" page will be added.
Open MyUserControl.ascx file.
Go to "Source" View
Add Asp Label form toolbox
<div id="divlbl"
style="background-color: #C0C0C0; font-family: Arial; font-size: medium;">
<asp:Label ID="lblTest" runat="server"></asp:Label></div>
I want to display SharePoint logged in user name in the above label.
Go to MyUserControl.ascx.cs
Add below code in page load
string strUserName = "Current Logged in User Name:" + SPContext.Current.Web.CurrentUser.Name;
lblTest.Text = strUserName;
Now the MyUserControl.ascx.cs code will be...
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace SharePointProject1.ControlTemplates.SharePointProject1
{
public partial class MyUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string strUserName = "Current Logged in User Name:" + SPContext.Current.Web.CurrentUser.Name;
lblTest.Text = strUserName;
}
}
}
Build and deploy the project.
Deployment Updates
This solution will create new folder in SharePoint server.
Go to 14 hive..
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\
You will find "SharePointProject1" folder
Inside that you can find "MyUserControl.ascx"
Add User Control to SharePoint Page
Now i want to add this User control to SharePoint home page, so that i can view ascx content.
I can add user control in different places in SharePoint,like in Pages, Master pages, Page layouts, and even in Web parts (use Page.LoadControl method)
Open SharePoint Designer, Open the target site.
Click on All files
Right Click on Default.aspx
Click on Check out
Open Default.aspx
IN the top ribbin select Advanced Mode
Register the user control at the top of the page. Before <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server"> tag... add below tag
<%@ Register TagPrefix="MyUserControl" TagName="UserName" Src="~/_controltemplates/SharePointProject1/MyUserControl.ascx" %>
Insert the user control wherever required.
Now, we can include the user control in the page, Here I've done like this:
<MyUserControl:UserName id="MyUserControl1" runat="server" />
I added in this section...
<tr>
<td class="ms-webpartpagedescription">
<SharePoint:ProjectProperty Property="Description" runat="server"/> <table><tr>My User Control<td><td> <MyUserControl:UserName id="MyUserControl1" runat="server"
/> </td></td></tr></table> </td>
</tr>
Save it.
Click on Preview in browser..
That's it..We can see your name in the web page....:)