Building my own UserControl WebPart part VI
Since the time Jan beat me by delivering his Return of the SmartPart, I still get lots of request to complete my initial blog about dynamically loaded User Controls in SharePoint 2007 / ASP.NET 2.0 Portals. Keep in mind SmartPart is a great WebPart, but focuses completely on SharePoint. The WebPart even inherits from Microsoft.SharePoint.WebPartPages.WebPart and isn't what I found able to use enumerators. Besides this I like to complete where I once started and Microsoft is still not delivering an additional component which they once promised. See a blog of Scott Guthrie where he describes some info about whether it will be possible to host Web Parts built as ASP.NET 2.0 User Controls within SharePoint 2007.
The last time I stuck at part V which was al about the WebPart EditorPart, to select a UserControl from the "UserControls" directory. This part of the blog series focuses on discovering the properties of selected UserControl. In this blog I won't be bother you with big code screen but only with three small ones. All other code will be included as attachment, so you can read and investigate it. You can learn a lot of debugging code. Most of the ASP.NET 2.0 portals uses personalization, which can be setup with the aspnet_regsql.exe command. This command can be found at %WINDOWS%\Microsoft.NET\Framework\v2.0.50727. After running this all portal users are able to store their settings of the given WebParts. I added the ability to store the properties of UserControl as well. To set and show the properties of a UserControl you can use the [WebBrowsable] and [Personalizable] attribute.
I divided the properties in three categories.
- String -> rendered as a TextBox
private string _textField;
[WebBrowsable(true), Personalizable(true)]
public string TextField
{
get
{
return this._textField;
}
set
{
this._textField = value;
}
}
- Boolean -> rendered as a CheckBox
private bool _boolField;
[WebBrowsable(true), Personalizable(true)]
public bool BoolField
{
get
{
return this._boolField;
}
set
{
this._boolField = value;
}
}
- Enumerators -> rendered as a ComboBox
public enum Days
{
Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
}
private Days _enumField;
[WebBrowsable(true), Personalizable(true)]
public Days EnumField
{
get
{
return this._enumField;
}
set
{
this._enumField = value;
}
}
Most of my classes kept the same signature compared to the initial design. For a complete description of all classes their methods, you can read the second post of the blog series.
Class Diagram
To use the UserControlWebPart assembly you must register it in your ASP.NET 2.0 portal application or populate it in SharePoint. A good guide to follow for populating the SharePoint WebParts Gallery is of Mart Muller. See http://blogs.tamtam.nl/mart/CreateASharePoint2007WebpartStepByStep.aspx. It's based on Office SharePoint Server 2007 Beta 1 TR, but still do the job. After this you can use it in any of these apps as a normal WebPart. Your UserControls need to be in the "UserControls" folder of the application. When a UserControl contains a separate assembly file, you must place it in the "bin" folder of the application.
UserControl in ASP.NET 2.0 Portal and SharePoint using my own UserControl WebPart
As you can see in the image, I now have create my an UserControl EditorPart which customizes / personalizes a UserControl.
Summary
I did it again. This time it costs me several days instead of hours, because retrieving and storing the properties of user control isn't easy. Maybe somebody can confirm this ;-), it would encourage me to finish the blog series. The next part will focus on setting up connectable user controls by using the consumer / provider model of ASP.NET 2.0. Frankly I don't have a clue when I post this blog because I'm working on setting up TFS to run over SSL and off course about WSS 3.0 the MS way. To give everyone including the SharePoint Team the ability to start using this WebPart I included the sources and DLL in the post as well. If somebody has better ideas (e.g. AJAX) or want to help writing this code, send me an email.