Monday, May 12, 2008

Deploy a user control(.ascx) as a SharePoint web part

Frustrated by the lack of control over the UI when creating SharePoint web parts, want something more lightweight than SmartPart. The following code snippet illustrates how you can deploy a user control as a SharePoint web part.

1) Place your complied code in either the GAC, or web application \bin folder.
2) Save your user control to the file system
(usually: "12\TEMPLATE\CONTROLTEMPLATES").
3) Replace the string "MyUserControlPath" with the location of the usercontrol.
4) Deploy web part as normal.

 
[Guid("9c974b27-d41a-4053-8eb2-76c25e762f1b")]
public class MyWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public MyWebPart()
{
this.ExportMode = WebPartExportMode.All;
}

protected override void CreateChildControls()
{
try
{
base.CreateChildControls();

this.Controls.Add((System.Web.UI.UserControl)
Page.LoadControl("MyUserControlPath"));
}
catch (Exception ex)
{
EventLog.WriteEntry("WebParts", ex.Message, EventLogEntryType.Error);
}
}
}

1 comment:

Unknown said...

Hi, There is also a downside to using too much images on your site in Web Design Cochin and that is it will take more bandwidth to load the whole web pages. Thanks.....