SmartPart for SharePoint 0.2.0.1 Released!
For those of you who don’t know the SmartPart for SharePoint; a little introduction: “A SharePoint web part that can host any ASP.NET user control. Create your web parts by using the VS.NET designer instead of coding everything by hand!”
In the last weeks Fons and I have worked on some cool new functionality, you can download the new release (version 0.2.0.1) from the GotDotNet Workspace. To install the SmartPart, execute the MSI Package that’s included. Due to some internal changes, the SmartPart assembly must be deployed to the Global Assembly Cache (GAC), hence if you’re prompted by the installer, choose ‘Yes’. For this release you need to manually alter the web.config file and set the trust level in the system.web section to WSS_Medium (see the readme file for more info). I’m working on this to avoid the manual intervention (already many thanks to Maxim for his help). So, what’s the cool new stuff in this release?
- Exposing user control properties in the properties toolpane. In the previous versions you needed to implement the IPropertyProvider interface so the SmartPart properties toolpane would display some properties of the user control it contained. These days are over, you can now add the Browsable attribute to every property you want to be exposed. That’s it! By using the Description attribute you can control the caption of the property in the toolpane. Both attributes are in the System.Componentmodel namespace.
- Dropdown list for easy selection of user controls. In the previous versions the location of the user control needed to entered manually (e.g. “~\UserControls\HelloWorld.ascx”). In the new version you can choose between two web parts: one similar to version 0.1.x (with a textbox as input) and another one with a dropdown list that contains all available user controls. By default the SmartPart List web part will look in the \UserControls directory which user controls are available. If you prefer another location, you can easily change it in the DWP file.
To illustrate how to use the new stuff and to show how easy it is, let’s create a small demo web part. Let’s create a web part that displays the name of the currently logged on user. In Visual Studio, create a new ASP.NET Web Application. Add a new Web User Control to the project, for example UserInfo.ascx. Add a reference to the SmartPart.dll, which is included in the download, and to the Microsoft.SharePoint dll, which you can find on your SharePoint server. Suppose we want to be able to let the user choose a prefix that should appear in front of the user name (e.g. “Current user John Doe”). So we’ll need to create a property Prefix and decorate it with the Browsable attribute. For a good user experience we also add the Description attribute to specify how the property is presented to the user. Add a Label control that will contain the actual text. Because we want to display the user that is currently logged on, we’ll need access to the SharePoint object model. That’s quite easy, just implement the SmartPart.IUserControl interface. Finally we need to add the code to display the prefix and username to the Page_Load function. To finish of, you can add a Description attribute to the class too, so it gets a nice name in the SmartPart dropdown list. The complete code of the user control could look like this:
[System.ComponentModel.Description("Hello World Demo")]
public class UserInfo : System.Web.UI.UserControl, SmartPart.IUserControl
{
private string _prefix = null;
protected System.Web.UI.WebControls.Label Label1;
private Microsoft.SharePoint.SPWeb _web;
[System.ComponentModel.Browsable(true),
System.ComponentModel.Description("Prefix to display")]
public string Prefix
{ get { return _prefix; }
set { _prefix = value; } }
public Microsoft.SharePoint.SPWeb SPWeb
{ get { return _web; }
set { _web = value; } }
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = this.Prefix + this.SPWeb.CurrentUser.Name;
}
#region Web Form Designer generated code
// Left out..
#endregion
}
After you’ve build the project, the deployment can start. You need to copy two files: copy the UserInfo.ascx to the \UserControl folder in the folder that maps to your SharePoint site (e.g. C:\Inetpub\wwwroot\UserControls) and copy the UserInfo.dll to the \Bin folder (e.g. C:\Inetpub\wwwroot\Bin). If the UserControls folder does not exist, you can create it (it’s not a default SharePoint folder). Now drag-and-drop an instance of the SmartPart List web part to a site, and choose the “Hello World Demo” control from the drop down list.