Part 4–SharePoint Retreat–TDD and Unit Testing, the methodology used cont..

The other parts of this series can be found here…

  1. SPRetreat TDD – a retrospective (See what I did there? -)
  2. Part 1 — SharePoint Retreat–TDD and Unit Testing, the methodology used
  3. Part 2 — SharePoint Retreat–TDD and Unit Testing, the methodology used cont..
  4. Part 3 -– SharePoint Retreat-TDD and Unit Testing, the methodology used cont..
  5. Part 4 –- SharePoint Retreat-TDD and Unit Testing, the methodology used cont.. (This post)

Putting it all together..

Over the last few parts, we’ve created and tested our Magic8Ball, we’ve extended it to become SharePoint aware and now we just need to wrap it up in a WebPart to finish this piece! For this we’re going to use the new SharePoint visual web part project template and aim to finish with this:-


We won’t be using any of our Unit testing tools now, But it’s worth just re-running all the tests again if you’ve closed Visual Studio since your last visit, “Confidence booster isn’t it?”

Right click the solution and choose Add/New project.



One down side of the visual Web Part project is the fact that we can’t use the Sandbox, so we’ll deploy as a farm solution to our chosen test farm.

You’ll now have a new project in your solution explorer.


Right click the References tag and add a project reference to the Magic8Ball assembly.

One thing we forgot to do is Strong name the Magic8Ball assembly. We have to do this if we want to deploy and reference that assembly in a SharePoint project, so go do that now!

We want to add a nice image of the magic 8 ball and we can let the deployment process handle that for us too. Right click the web part project and choose Add/SharePoint mapped ‘Images’ folder. This adds a new folder to the project and more importantly adds it to our feature, ready for deployment. Find yourself a nice picture of a magic 8 Ball. I used one from a free clip art site found on Bing. Drop this file into the sub folder that Visual Studio added to the mapped image folder.


That’s all the additional content and references we need, so now we can drop into the designer and build our web part visually. I added the following in this order (top to bottom):-

  1. An asp panel (to hold everything)
  2. An image control. (Use the browse function on the ImageUrl property to brose to the mapped folder. This will set the URL correctly for the mapped folder.)
  3. A label (for the answer to our all important question)
  4. A textbox (So we can ask our question)
  5. A button.

I’ll leave you to work out how to lay them out as I have done below..


Double click on the button and visual studio will switch to the code view where you can add the action needed to make the ball function.

Add the code below to your click event (Varying the textbox and label ID’s for your own of course!)

            SharePoint8Ball spBall = new SharePoint8Ball("8BallAnswers");
            TheAnswer.Text = spBall.AskQuestion(QuestionBox.Text);

That’s the code functionality finished, now we just need to make sure everything is packaged up for deployment to SharePoint.

In the solution explorer, expand the tag marked package and double click on the file package.package to bring up the solution package wizard. Don’t change anything on this page, but click on the advanced tab at the bottom. This will open the additional assemblies dialog. Click Add., then Add assembly from existing project output. Choose Magic8Ball as the source project.


This will ensure that your Magic8Ball.dll is deployed as part of the solution. Click OK.

Browse to the VisualWebPart1 folder and open the .webpart file. Change the title and description properties to something more meaningful.

Now click Build/Deploy Magic8Ball Web Part.

Now browse to your test site and edit the page. Click add a web part. You’ll find your web part under the ‘Custom’ tab.

Add to the page, save and enjoy!


PS, I had a slight accident while cleaning up the code, so once I’ve recreated the accidently deleted project I’ll add a link to the code to this post.

PPS, I realised that in my rush to get this post finished during the week, I didn’t actually meet our use case completely. The intention was to give the Administrator the ability to set the list name through the web part properties and use that in the code tor etrieve the items.

I’ll aim to get the deleted code re-written this week and include the properties interaction at the same time.

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.