Feed Icon  


  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics


Open Source Projects



Test Driven SmartPart Development

Posted in SharePoint | ASP.Net/Web Services | Test-Driven Dev at Wednesday, December 8, 2004 3:02 AM Pacific Standard Time

A couple of weeks ago Jan Tielens released the SmartPart version The SmartPart is a very cool WebPart that will host ASP.Net user controls in a SharePoint environment. This means you can use Visual Studio to edit the user controls and use the WYSIWYG editor instead of building the HTML via code. Additionally, Maxim Karpov helped out with the CAS aspect of the SmartPart which can be a confusing topic if you're not Maxim. Maxim does a great job of explaining the CAS issues and even though I've worked with CAS for things like custom code in reporting services, I still feel it is something foriegn (I think CAS knowledge seeps out of the brain).

So the SmartPart allows you do quickly create SharePoint WebParts and you can even debug those WebParts in a normal ASP.Net application, but what about using Test Driven Development (TDD)?

Well Scott Hanselman gave me an idea that I thought would be worth trying out. Scott detailed how to use TDD for normal ASP.Net development using Cassini, an idea I found to be very cool. So could I use the same technique to use TDD for SmartParts? The answer is you can.

First I created a SmartPart project to build my sample SmartPart. To do this I used a regular class library project since this will give me everything I need without having to use a web server. You will need to add references to System.Web, Microsoft.SharePoint, and SmartPart. For my simple SmartPart I created one ASCX file and one CS file. In my user control I added a Label and in the Page_Load event handler I set the Text property to “This is a simple smart part.”.

The next step is to create your testing project. Again I created a class library project and this time added references to the Cassini (using the cassini.dll) and NUnit.Framework (I'm using NUnit 2.2). I had to make some modifications to Scott's code in order to make things work since my approach is slightly different. He is testing ASPX pages while I'm testing a user control that needs to be loaded in an ASPX page.

The first test was just to start the cassini server and open an ASPX page that had no control to make sure everything worked. That was fairly easy thanks to Scott. The next step was a little more involved since I had to copy the user control and the dll from my other project and reference the ASCX in the ASPX page. However, after some trial and error I got that working as well. The real fun came when I went to set the SPWeb property of my SmartPart as shown by Jan in his debugging post.

The SPGlobalAdmin class requires the Microsoft.SharePointLibrary.dll in order to function (which exists only in the GAC) [Update: I made a change here since I made a mistake in the original post. The SPGlobalAdmin is in the Microsoft.SharePoint.Administration namespace, not library.]. I remembered seeing a post about how to extract this from the GAC so after a little googling I came across this example which allowed me to extract the dll and add it to the bin folder of my cassini project. Once I had this dll accessible to cassini things started to work except for a security exception: Failure decoding embedded permission set object. Hmmm.

This one kept me baffled for quite some time until I got the bright idea this morning (after reading some of Maxim's CAS articles again) to take a look at the constructor for the SPGlobalAdmin class using Reflector. Using Reflector I noticed that the contructor demanded the permission Microsoft.SharePoint.Security.SharePointPermission. Ahhh. So back to the GAC again to get Microsoft.SharePoint.Security.dll.

Once I had that everything worked. All my tests passed and I'm able to start developing my SmartParts using TDD. Now I'm off to see if I can figure out how to test creating and using connections. If people are interested I can write a short article with the code I used to do this.

Update: I ended up running into issues when setting the SPWeb property. I was able to get the SPGlobalAdmin but nothing past that. But at this point I'm not sure how much that matters. I did get some very simple connections working just by using Part1.SetConsumerData(Part2.GetProviderData()).

Wednesday, December 8, 2004 4:38:00 AM (Pacific Standard Time, UTC-08:00)
I'd like to see more information and maybe an example on how you got this working.
<br>Nice job.
Monday, December 13, 2004 1:32:00 AM (Pacific Standard Time, UTC-08:00)
Wednesday, December 15, 2004 3:59:00 AM (Pacific Standard Time, UTC-08:00)
<br>Great idea, nice job!
<br>[www.ipattern.com do you?]
Wednesday, December 22, 2004 1:39:00 PM (Pacific Standard Time, UTC-08:00)
You mean there are people on this planet who understand what you said there?
Monday, May 30, 2005 11:43:00 PM (Pacific Daylight Time, UTC-07:00)
The reason you can't do anything after the 'SPGlobalAdmin' part is that you cannot remotely access the SharePoint OM.
<br>Jan's code works find as long as you run it on the SharePoint server itself.
<br>I suppose you could probably get round this using Web Services.
Colin Anderson
Tuesday, October 18, 2005 7:20:00 PM (Pacific Daylight Time, UTC-07:00)
Hi bryant ,
<br>Can you please explain how you did u test the connected smartparts in a aspx page.
Wednesday, February 22, 2006 10:06:28 PM (Pacific Standard Time, UTC-08:00)
I'm deploying my usercontrols as webparts on my sharepoint portal using SmartPart, can you please tell me how to make my webparts connectable meaning two SmartPart webparts to connect to each other??
Wednesday, May 14, 2008 11:11:01 PM (Pacific Daylight Time, UTC-07:00)
i had placed Usercontrols with Smartpart in WSS 3.0, Connected the Smartpart , but when i navigate from one page to another & come back...

All my Data selected is lost & it fires SetConsumerData()

Please guide me if iam doing wrong here.

Friday, July 11, 2008 12:55:50 AM (Pacific Daylight Time, UTC-07:00)
I know knowledge is precious but couldn't you share with us details of your explendid work? For instance, a sample project would help.

Thanks in advance.
Thiago Bortot R. Chagas
Saturday, August 30, 2008 9:19:55 PM (Pacific Daylight Time, UTC-07:00)
Bryant, I know this is an old post but it still comes up on searches so I though it use to post a link to a white paper we have justed release Beginners Guide to Test Driven Web Part Development.


This white paper forms part of our Unit Testing SharePoint Solutions - The Basics series and provides an introduction to unit testing web parts within SharePoint 2007; this is a beginner’s guide and as such does not aim to address some of the more challenging issues you will face as you develop your SharePoint solutions (we will be covering these in our future more advanced white papers) but aims to get you thinking about how to develop with testability in mind.
Comments are closed.