Web Parts

13 Comments

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.NETFrameworkv2.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.

  1. String -> rendered as a TextBox
    private string _textField;
    [WebBrowsable(true), Personalizable(true)]
    public string TextField
    {
        get
        {
            return this._textField;
        }
        set
        {
            this._textField = value;
        }
    }
  2. Boolean -> rendered as a CheckBox
    private bool _boolField;
    [WebBrowsable(true), Personalizable(true)]
    public bool BoolField
    {
        get
        {
            return this._boolField;
        }
        set
        {
            this._boolField = value;
        }
    }
  3. 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.

 

As I've written in a previous blog:

I recently checked a couple of blogs about using dynamically loaded User Controls. Some of the blogs mentioned SmartPart v2 (aka SonOfSmartPart) to load ASP.NET 2.0 User Controls. This WebPart is completely targeted to SharePoint 2003, but it will remain working in MOSS 2007. When creating new Web Parts, you have the option of creating Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart (inherits from). The Windows SharePoint Services WebPart class exists primarily for the purpose of backward compatibility (Web Parts continue to work in Windows SharePoint Services 3.0 without modification), and to provide a small set of features that are not available in the other class.

Besides the targeting it's also not possible to use this Web Part in a ASP.NET 2.0 Portal. I know it's really easy to drag and drop a User Control in ASP.NET 2.0 Portal, but I also want to have this ability in MOSS 2007. Furthermore I like to have an option to add User Controls dynamically (not included in my Portal project). Scott Guthrie has written some astonishing info in one of his blogs about whether it will be possible to host Web Parts built as ASP.NET 2.0 User Controls within SharePoint 2007. He checked with the SharePoint team, and they told him that this will be a supported scenario via an additional component you add to SharePoint and that they'll have a Whitewater and sample on how to-do this published later this year.

I had heard some rumors that Jan Tielens was writing a Revival of SmartPart, but I couldn't wait that long. So I started to write a series of posts about my own UserControl WebPart. The goal of the series is to create a DLL which is public to everyone who wants to use it. But before I could end the series Jan posted the following blog. 

Via Jan:

Finally the latest version of the SmartPart is here: the Return of the SmartPart, also know as SmartPart v3, also know as SmartPart for SharePoint 2007. For those of you who are new to the SmartPart: the SmartPart is an open source web part that can host any ASP.NET Web User Control. What is the advantage of creating web parts by making use of the SmartPart? Well if you are familiar with the "traditional" web part development technique you probably know that you have to write lots of code, there is no designer support in Visual Studio. The SmartPart allows you to create a Web User Control (ASCX) in Visual Studio by making use of the designer, and deploy it to a SharePoint site. This release of the SmartPart is targeted for SharePoint 2007, both Windows SharePoint Services v3 (WSSv3) and Microsoft Office SharePoint Server 2007 (MOSS). Since the initial release almost 40.000 SharePoint developers have downloaded the SmartPart, get your copy today (for free of course) from the GotDotNet Workspace (in the releases section)! For those of you who have attended my session about web part development on TechEd: Developers in Barcelona, this is the version that I showed over there.

For more information about the deployment of the Return of the SmartPart, check out this post which also has a link to a small screencast. Keep an eye on my blog or www.smartpart.info for more information, examples and screencast about creating web parts the smart way!

Besides the SmartPart I still hope to finish my own UserControl WebPart.

 

Technorati tags: , ,

6 Comments

I recently checked a couple of blogs about using dynamically loaded User Controls. Some of the blogs mentioned SmartPart v2 (aka SonOfSmartPart) to load ASP.NET 2.0 User Controls. This WebPart is completely targeted to SharePoint 2003, but it will remain working in MOSS 2007. When creating new Web Parts, you have the option of creating Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart (inherits from). The Windows SharePoint Services WebPart class exists primarily for the purpose of backward compatibility (Web Parts continue to work in Windows SharePoint Services 3.0 without modification), and to provide a small set of features that are not available in the other class.


Besides the targeting it's also not possible to use this Web Part in a ASP.NET 2.0 Portal. I know it's really easy to drag and drop a User Control in ASP.NET 2.0 Portal, but I also want to have this ability in MOSS 2007. Furthermore I like to have an option to add User Controls dynamically (not included in my Portal project). Scott Guthrie has written some astonishing info in one of his blogs about whether it will be possible to host Web Parts built as ASP.NET 2.0 User Controls within SharePoint 2007. He checked with the SharePoint team, and they told him that this will be a supported scenario via an additional component you add to SharePoint and that they'll have a Whitewater and sample on how to-do this published later this year.


I couldn't imagine this option wasn't available in MOSS 2007 and that the SharePoint team will give a additional component only later this year. As Joris Poelmans wrote in his blog Usercontrols (ascx), webparts and SharePoint 2007, it isn't that hard to dynamically load a User Control. Reading this blog I decided to write my own SmartPart fully ASP.NET 2.0 compatible. I heard some rumors that Jan Tielens is writing a Revival of SmartPart too, but I couldn't wait that long.


I'm starting a series of posts about my own UserControl WebPart. The goal of the series is to create a DLL which is public to everyone who wants to use it. While other articles sometimes are too long, I want to keep them very short. That way you're able to read them before your workday begins or perhaps just before you shut down. 🙂


From every post I'll refer to this post and I'll keep an index here so you can easily find your way.

Complete index


  1. part I (Startup project)
  2. part II (Class Diagram and test UserControl project)
  3. part III (WebPart project)
  4. part IV (About EditorPart, displaying a info about the WebPart)
  5. part V (WebPart EditorPart, selecting a UserControl)
  6. part VI (UserControl EditorPart, discovering properties of UserControl)
  7. part VII (Consumer / Provider)

I try to keep you informed and eventually will post my UserControl WebPart as a DLL.