Tuesday, May 12, 2009

Smartpart in SharePoint (MOSS) 2007 - Easy Web User Controls

The SmartPart is a generic webpart that can contain an ASP.NET user control. Nothing new you would say, but the SmartPart can give your user control access to the SharePoint object model.

Smart Part allows developers to code simple (or complex) Web User Controls (.ascx files) utilizing the full set of visual development tools in VS and then deploy the result to a SharePoint (MOSS) 2007 site by just configuring the url of the user controls.

Need for SmartPart and it's benefits

  • The SmartPart is a SharePoint Webpart that can host any ASP.NET user control. Creation webparts by using the VS.NET designer(user control) instead of coding everything by hand.
  • It complies with sharepoint object model since it's a type of web part.
  • Easy to deploy user controls in MOSS sites
  • Simple configuration
  • Supports AJAX and hence we can create AJAX enabled Smartpart eaily.

The following steps shows the steps to create and deploy the smartpart in MOSS site

Step 1: Create a SmartPart
TO create a smartpart, we need to create a library project using visual studio editor and add the following code. Add the reference to Microsoft.SharePoint.dll.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace SmartpartSample
/// <summay>
/// Description for UserControlContainer.
/// </summay>

ToolboxData("<{0}:UserControlContainer runat=server>"),
XmlRoot(Namespace = "SmartpartSample")]
public class SmartpartSample : Microsoft.SharePoint.WebPartPages.WebPart
private const string defaultText = "";
private string _userControl = defaultText;
private Control _control = null;

Category("User Control"),
FriendlyName("User Control (.ascx)"),
Description("Path to the User Control (.ascx)")]

public string UserControl
// This property has the URL of the user control to be displayed in the web part
return _userControl;
_userControl = value;
/// <summay>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.

///An array of references to ToolPart objects.
/// </summay>
public override ToolPart[] GetToolParts()
// These tool parts are to set the properities of the smart part
ToolPart[] toolparts = new ToolPart[2];
WebPartToolPart wptp = new WebPartToolPart();
CustomPropertyToolPart custom = new CustomPropertyToolPart();
toolparts[0] = custom;
toolparts[1] = wptp;
custom.Expand("User Control");
return toolparts;

/// <summay>
/// Load the UserControl
/// </summay>

protected override void CreateChildControls()
if (_userControl != defaultText)
_control = this.Page.LoadControl(_userControl);
_control = new LiteralControl(string.Format("To link to content, open the tool pane and then type a URL in the Link text box.", 1, 129, this.ID));
catch (System.Exception ex)
_control = new LiteralControl(string.Format("Error: unable to load {0}
Details: {1}", _userControl, ex.Message));
if (_control != null)
// Add to the Controls collection to support postback

/// <summay>
/// Render this Web Part to the output parameter specified.
/// </summay>

/// The HTML writer to write out to
protected override void RenderWebPart(HtmlTextWriter output)
if (_control != null)

Step 2: Adding the smart part into MOSS Site
1. Place the dll of the smart part project into the bin folder present in the virtual directory folder of the MOSS site.
2. Add a safe control entry for the smart part in the web.config file of the MOSS Site.

<SafeControl Assembly="Assembly Name of the SmartPart, Version=, Culture=neutral" Namespace="Namespace of the SmartPart" TypeName="*" Safe="True" />

3. Modify the trust level to WSS_Medium in the web.config as shown below.
<trust level="Full" originUrl="" />

4. Open the SharePoint portal where the web parts have to be deployed.

5. Click on Site Actions->Site Settings->Modify All Site Settings

6. Click on Web parts option under Galleries

7. Click on New tab on the Web Part Gallery page.

8. Click on the checkboxes against the Smart part which we have deployed using the above mentioned steps.

9. Click the “Populate Gallery” button.

Step 3: Placing the User Controls and link to MOSS site

1. Copy the User controls to the UserControls folder in the Virtual directory of the MOSS Site.

2. If the UserControls folder is not available create the same.

Step 4: Using the smart in the site pages

1. Create a new page in the MOSS site and edit the page.

2. Add the Smart Part to the page and edit the properties of the smart part.

3. In the properties window of the smart part provide the user control path to be loaded in that page.

4. The path of the usercontrol file should be in this format “~\UserControls\UserControl File Name”.

5. Click on OK button, now the User control should be visible on the page.

6. Exit the page edit mode.

1 comment:

aparna john said...

Hi, A site designed for broadband users can be graphics intensive compared to a site designed for dial-up users in Web Design Cochin who typically use a slower Internet connection.Thanks.....