Friday, 15 April 2011

How to create a custom Web Part for SharePoint

I’ve been working recently with Darren Pritchard at a site that’s fairly new to SharePoint. They came up with a number of fairly basic ‘how to’ questions and Darren has put together some basic information for them. It seemed that if these new SharePoint users and had lots of questions, then so would many other sites. So it made sense to make this information available to the SharePoint community as a whole. This site was using SharePoint 2007.

The first thing to do when creating a custom Web Part is to set up a development environment. You need to create a development SharePoint 2007 (SP2007) server and give it a name. For SharePoint 2007 you need Visual Studio 2008 (VS2008). It’s worth noting that you can’t use Visual Studio 2010 (VS2010).

Install VS2008 onto your development SP2007 server. Next install the VS2008 SharePoint plug-in called “VSeWSSv13_AMD64_Build-433.exe”. This can be found at http://www.microsoft.com/downloads/en/details.aspx?FamilyID=fb9d4b85-da2a-432e-91fb-d505199c49f6. This plug-in adds the Web Part project to VS2008. Once this is in place, you’ll be able to actually create a custom Web Part.

Open VS2008, then click ‘File/New/Project’. Find the ‘Visual C#’ list and expand that. Next click ‘SharePoint’, and in the right-hand box click ‘Web Part’. Give your new project a name. The naming convention is og.wp.%Name%, where:
og = your organization (abbreviated, eg use gs if you’re company is called ‘Great Shoes’, etc.)
wp = Web Part
.%Name% = the name of the Web Part.

Make sure you tick ‘Create directory for solution’ – see Figure below.


Next you’ll see a trust level box. Make sure that it looks like this:



And click ‘OK’.

You can now go ahead and write your Web Part using Visual C#.

The next stage shouldn’t be missed – it’s testing your custom Web Part. You need to tell VS2008 where to deploy the Web Part. Click ‘Project’, ‘%PROJECT NAME% Properties’. On the tabs click ‘Debug’ and change the ‘Start browsers with URL’ box to point to your test site, eg http://yourdevelopmentserver:5003/. Click the cross in the right hand corner to save the changes.

To deploy your Web Part, click ‘Build’, ‘Deploy %PROJECT NAME%’. This will add the solution to SharePoint central admin and deploy the solution to your test site. You can add your Web Part to your test site in the same way that you would add any other Web Parts, and now you can test it.

It’s useful to know how to retract a test Web Part. Open your project in VS2008, click ‘Build’ and then ‘Retract Solution’. This will retract the solution from your test site and remove it from SharePoint central admin. You now need to manually remove the pointer from your test site. To do this open your test site and click ‘Site Actions/Site Settings/Modify All Site Settings’. Under Galleries click ‘Web Parts’ and delete your development Web Part.

Assuming that your Web Part worked as planned (finally!), you’ll want to deploy your solution into your live environment. Firstly, locate your project, eg ‘C:\Users\%USERNAME%\Documents\Visual Studio 2008\Projects’, then double-click on your new project, and locate the debug folder. Within this you should find %PROJECT NAME%.wsp. Finally copy this .wsp file onto your Live SharePoint Server.

I’ll share Darren’s full instructions for installing a solution another time.