Saturday 13 August 2011

Deploy User control in sharepoint 2010


Steps to display a User control in a webpart in Sharepoint 2010. The solution will also deploy the user control in _layouts folder in Controltemplates

1. Create a visual webpart
2. Add the below code to load the user control
namespace SimpleWebPart.VisualWebPart1
{
public class VisualWebPart1 : WebPart
{
protected const string _ascxPath = @”~/_CONTROLTEMPLATES/SimpleWebPart/VisualWebPart1/CustomUserControl.ascx”;
public VisualWebPart1()
{
}
protected override void CreateChildControls()
{
try
{
Control control = this.Page.LoadControl(_ascxPath);
Controls.Add(control);
}
finally
{
base.CreateChildControls();
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}
3. Next we will add the user control in our webpart solution so that it gets deploy along with the webpart.
4. Next right click on the project and navigate to “Add -> SharePoint Mapped Folder”.
5. In the popup select TEMPLATE -> CONTROLTEMPLATES.
7. Once done you will see a new CONTROLTEMPLATES added to your project solution. You can now add your control CustomUserControl.ascx in your control templates folder.
8. Next press F5 to deloy the project.
9. Verify in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES location for your custom control CustomUserControl.ascx



Ads by Google

No comments:

Post a Comment