Debugging SmartPart Webparts Without Deploying to SharePoint

A quick intro for those of you who haven’t heard of the SmartPart for SharePoint: the SmartPart is a SharePoint Webpart that can host any ASP.NET user control. Create your webparts by using the VS.NET designer instead of coding everything by hand!

 

One of the advantages is that when you use ASP.NET user controls to create Webparts, you can easily test your user control inside an ASP.NET web application instead of deploying it each time to SharePoint. When you create the user control, just drag-and-drop it onto a blank ASP.NET form for example and hit F5. Of course you miss the SharePoint layout, but to solve that you could quite easily include the CSS in you ASP.NET project.

Those of you who already used the SmartPart probably know that you can notify your user control of the SharePoint context in which it’s displayed. You need to implement the SmartPart.IUserControl interface which is used to pass around an instance of the current SPWeb. A very basic user control that displays a dropdown navigation for all your subsites could look like this (don’t forget to add references to the Microsoft.SharePoint dll and the SmartPart dll):

 

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint;

 

public class DropDownNavigation : System.Web.UI.UserControl, SmartPart.IUserControl

{

            protected System.Web.UI.WebControls.DropDownList DropDownList1;

           

            private SPWeb _web;

 

            private void Page_Load(object sender, System.EventArgs e)

            {

                        DropDownList1.Items.Clear();

                        DropDownList1.Items.Add("Select a subweb ...");

 

                        foreach(SPWeb subWeb in this.SPWeb.GetSubwebsForCurrentUser())

                        {

                                   DropDownList1.Items.Add(new ListItem(subWeb.Title, subWeb.Url));

                        }

            }

 

            #region Web Form Designer generated code

            // left out …

            #endregion

 

            private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

            {

                        this.Response.Redirect(DropDownList1.SelectedValue);

            }

 

            public SPWeb SPWeb

            {

                        get

                        {

                                   return _web;

                        }

                        set

                        {

                                   _web = value;

                        }

            }

}

 

But if you drop this user control on an ASP.NET web form, you’ll get an null reference exception because the SPWeb property isn’t set. So would you have to deploy this user control to SharePoint in order to be able to test it? Luckily the answer is no! You can drag-and-drop the user control on an empty web form, and set the SPWeb property. You can get a hold of an SPWeb instance by using the SPGlobalAdmin class like this:

 

public class WebForm1 : System.Web.UI.Page

{

            protected DropDownNavigation DropDownNavigation1;

 

            private void Page_Load(object sender, System.EventArgs e)

            {

                        SPGlobalAdmin adm = new SPGlobalAdmin();

                        SPVirtualServer vs = adm.OpenVirtualServer(new Uri("http://w2003base:8000"));

                        SPWeb web = vs.Sites["sites/webpartday"].RootWeb;

                        DropDownNavigation1.SPWeb = web;

            }

 

            #region Web Form Designer generated code

            // left out

            #endregion

}

 

Additionally, to make this work:

  • Enable impersonation in the web.config of your web application by adding the Identity tag:
    <configuration>   
      <system.web>
        <identity impersonate="true"/>
        ...
  • Make sure the identity of the application pool that is running your web application has read access to the following registry key:
    SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\Secure\ConfigDb
    If you select Local System for example, as identity of the application pool, you should be fine.
    J

No Comments