Web User Controls + Webpart = MiniApp

19 12 2007

If you have implemented WSS or MOSS solutions, chances are that you have been asked to add some application-like functionality to the Sharepoint offer. This can be accomplished in several ways, but the one I have to meet my requests is to deliver a Web User Control (WUC) that will be loaded by a Sharepoint Webpart.

This way, you can develop you WUC anywhere (no need for WSS or MOSS installed) and later on encapsulate the call to the control in the Webpart.

To simplify the development process, I highly recoment the use of the Smartpart ( a webpart that has the ability to load Web User Controls and display them). For this you will need to create a folder in you WSS/MOSS web site called “UserControls”, where you should put all the WUC you will use. I don’t usually develop the WUC directly in this folder. It is usually best to develop the WUC in a Web Site test project and when all the complexity of the control is implemented I deal with the minor adjustments needed to make it work in the Sharepoint environment. The main advantage I see in this process is a simpler debug process as sometimes the errors in WSS/Moss are not very explicit.

Here is a basic method list I currently use:

    1. In Visual Studio, I create a test web site with a folder called “UserControls”.
    2. I add a WebUserControl to the UserControls folder and add the WUC to the /default.aspx page, so I can see test the progresses I am making
    3. Develop all the logic of the WUC
    4. In Sharepoint Central Administration, I create a new Web Application and a new site collection with the Blank Site template (ust for integration testing purposes)
    5. Install the SmartPart
    6. Open your new site and give your administrator credentials
    7. Chose “Site Actions »  Site Settings » Modify All Site Settings” and choose “Site collection features”. Activate the smartpart feature
    8. Create or edit a webpart page
    9. Add the smartpart to a webpart zone
    10. Configure the smartpart and select the WUC from the list. If your list is empty, then there is a problem with your smartpart installation or WUC placement. Refer to the smartpart documentation to try and solve the problem

For you to fully understand the steps above, some experience in WSS/MOSS is required. Drop me a comment if you need more detailed steps..

 This will put your WUC in Sharepoint. Usually you have some issues to deal with before all is up and running. I will try to enumerate some of the things that I had to fight against:

  • Query strings for WUC internal database connection: this held my breath for some time. After my WUC was up and running in my aspx test page, I imported it as described above and a sharepoint error page with the message “Trusted Connection can’t be used”. After some digging I found some posts on the subject and apparently this only happens if you use integrated windows authentication in the connection string (I can’t confirm this as I haven’t tested it). SQL Authentication was out of the question, and I had some databounded gridviews, select boxes, etc, so this had to work. The solution was (thanks to Miguel Barros for the help) to manually build the SqlDataSource objects and assign them to the controls. this required the creation of the OnInit method inside the .cs file.
  • MultiView control can’t be used in a WUC inside a webpart. Instead I used Panel control.
  • Exception management: this is crucial as some (yes some, that is how detailed I can go on this) cause the “Unexpected error” page and that’s it.
  • Debug, debug, debug. Use Attach to Process in Visual Studio to attach to the w3wp.exe process (several are usually running, so attach to all of them). This will allow your code to be debugged.

You may need to create your own webpart for loading the WUC. I’ll try to post a simple formula for doing so.

Hope this helped. I know there is not a lot of detail, but drop me a line if you need more detail or code samples and I’ll upgrade the post.




2 responses

8 01 2008

You mentioned that the MultiView control cannot be used in a WUC inside a webpart. I’ve tried out a rudimentary Multiview with 4 views. I could get it to work in MOSS and successfully transition between screens, with state management. It’ll be great if you can let us know the specific scenarios where this doesn’t function as expected.

Thanks in advance!

8 01 2008

Hi Borg,

so, I have created a WUC called testMultiview.ascx with the code:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="testMultiView.ascx.cs" Inherits="Controls_testMultiView" %>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
This is View 1<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Next" /></asp:View>
<asp:View ID="View2" runat="server">
This is View 2<br />
<asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Text="Next" /></asp:View>

on the code behind testMultiView.ascx.cs added the event processor for the button:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Controls_testMultiView : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)

protected void Button1_Click(object sender, EventArgs e)
if (MultiView1.ActiveViewIndex == 0)


Next, created a blank site (Colaboration), activated the SmartPart (for loading the control) and when tried to load the WUC I got an error message about the usage of multiviews.

At the moment I lack the test environment, but as soon as I get my hands on it I’ll post the remaining details on this issue.

Thanks for your interest!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: