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

 

13 thoughts on “Building my own UserControl WebPart part VI

  1. Hi Your post is really very useful. But, one thing i want to discuss with you is that, I have used smartpart for implementing user control but it has the folloiwng problem:

    I have one smart(having user control) part which is a producer on PageNo1.

    Secondly, I have another smartpart(having user control) on PageNo.2 which is a consumer for smart part on PageNo1.

    Now, the problem is that I am finding "Connections" link coming disabled showing tooltip something as "Connection type not compatiable with other parts of the web page."

    Now the catch is it works prefectly fine when both producer and consumer are on the same page, but does not work if put on different pages.

    My second big concern is that on "Submit" click, I want to move to next page and then show the consumer data fetched from producer on PageNo1.

    Please Suggest. It is too critical for my application.

    Reply
  2. jinal

    From where can i download this code of this ?

    I am facing problem to store Selected UserControl Properties.

    It display temporarly but when i start application next time ,it will not persists properties value.
    Waiting for your reply...

    Give me idea...

    Thanks

    Reply
  3. Gianluca

    Hi,

    Great tutorial!
    I've tried to download the attachments, but it was broken, can you check it?

    Thanx

    Reply
  4. wss

    Hi,
    Thanks for this tutorial. Great job!

    The attachment file is damaged, could you publish the file again?

    Best Regards.

    Reply
  5. Mike Glaser

    All,

    I reuploaded the file. The project structure is based on VSeWSS. So if you want to open it easily download it.

    Reply
  6. yuks

    Hi, this is a great tutorial!!

    Thanks for sharing such a powerful function of SharePoint webparts.

    However, I'm facing a problem here.

    I tried creating 2 of these web parts on the same page, each hosting a different UserControl.

    Say, UserControl1 has the following properties: Name, Age and Gender, whereas UserControl2 has the following properties: Page, Line, Column.

    When i saved the properties of UserControl1 in Webpart1 as "Noel" for Name, "12" for Age and "Male" for Gender, the properties of UserControl2 will share the same properties as well.

    Is there any way to fix this?

    Thanks

    Reply
  7. sara

    Nice !!!!!!!!

    Create Custom SharePoint WebParts, it is simple.
    Try this too,
    [Custom SharePoint WebParts][http://sarangasl.blogspot.com/2009/09/custom-sharepoint-webparts.html]

    Reply
  8. Jonnydock

    This UserControl WebPart is awesome! This is the only way I have found to successfully use and ASCX user control as a webpart. Is Microsoft ever going to give us a component to do this?

    Reply
  9. Jonnydock

    When you select an ASCX and it shows the message "Cannot save the property settings for this Web Part. One or more errors have occurred. See below for details." I don't see any thing with errors below that. Where do I look? The web part shows correctly to the left, but for some reason won't let me save. My other ASCX controls work fine. Thanks for your help.

    Reply
  10. J

    Interesting article, just so I'm on the right page, all this allows a SharePoint developer to write a UserControl? This in turn allows the developer access to a designer view of his/her control that he/she wants to upload the server?

    2 questions I have:
    1) Where do you create the '/UserControls' folder referenced earlier? Is it located somewhere in the 12 hive like 'C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATE'?

    2) I am using Windows SharePoint 3.0 Tools Visual Studio 2008 Extensions 1.2 and when I go to deploy the project MG.UserControlWebPart I get an error saying that: 'Cannot find this file specified in the manifest file: UserControlWebPartelementManifest.xml'. Am I using the wrong VSeWSS version?

    J

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *