Monday, December 06, 2004 9:19 PM Jan Tielens

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
Filed under:

Comments

# Test Driven SmartPart Development

Wednesday, December 08, 2004 8:02 AM by TrackBack

# Test Driven SmartPart Development

Wednesday, December 08, 2004 9:50 AM by TrackBack

# Test Driven SmartPart Development

Wednesday, December 08, 2004 9:58 AM by TrackBack

# Test Driven SmartPart Development

Wednesday, December 08, 2004 1:43 PM by TrackBack

# re: Debugging SmartPart Webparts Without Deploying to SharePoint

Friday, September 29, 2006 2:52 PM by Jeff Pickard

Can you debug user controls (breakpoints ..) while they run inside Son Of SmartPart in a SP context?

How would I do that?

Thanks

jean-francois.picard@cactuscommerce.com

# re: Debugging SmartPart Webparts Without Deploying to SharePoint

Monday, December 11, 2006 2:04 PM by Brad Hughes

Jan when I try to duplicate this I get "COM object with CLSID {BDEADEBD-C265-11D0-BCED-00A0C90AB50F} is either not valid or not registered". Am I missing something?

# re: Debugging SmartPart Webparts Without Deploying to SharePoint

Thursday, June 14, 2007 11:46 AM by Raghu

----------------------

When I try to duplicate this I get "COM object with CLSID {BDEADEBD-C265-11D0-BCED-00A0C90AB50F} is either not valid or not registered".

----------------------

I get the same error too, and I've posted this in experts-exchange.com and nobody seems to know the answer.... Could someone help me figure this out also?

THanks,

Raghu.

# re: Debugging SmartPart Webparts Without Deploying to SharePoint

Tuesday, June 26, 2007 7:19 AM by Govardhan

Hi,

I am using the code snip u provided, but when i tried to execute the application i get a exception "Input string was not in correct format",

Can u let me know wht would be the problem and help me to solve this problem. It woul be of great help if u could mail at my mail id. apondu@gmail.com

Thanks for the help,

Regards,

Govardhan

# Lexapro.

Tuesday, July 22, 2008 8:28 AM by Lexapro.

Lexapro for panic attacks. Lexapro side effects. Lexapro. Lexapro medication adverse effects.

# Lexapro &raquo; betty butterfield lexapro

Sunday, August 03, 2008 10:06 AM by Lexapro » betty butterfield lexapro

Pingback from  Lexapro &raquo; betty butterfield lexapro