Pryderi's notes

SharePoint 2007 - Creating a Hello World Web Part

To start some real custom development for SharePoint 2007 we need to be able to create a simple Web Part in Visual Studio 2008 and include it in our SharePoint site.

In this example I'm using Visual Studio 2008 on XP on my laptop, and SharePoint 2007 on a remote Windows 2003 Standard Server.

So create a Class Library project in Visual Sudio, add a reference to System.Web, and enter the code below.

We also need to update AssemblyInfo.cs in order for the Web part Assembly to be built in such a way that SharePoint can use it.

Now comes the tricky bit (tricky for me anyway). SharePoint will only use this Web part if the assembly DLL has a strong name.

So we have to sign the assemnbly. Right-click on the project, select properties, and then click on the Signing tab. Check the box next to 'Sign the assembly', then select New from the drop-down box.

In the Create Strong Key Name box enter a name for a key file, and enter a password (twice).

So now you should have the following screen.

The next step is to compile the Assembly directly into the bin directory of your default SharePoint site on the server. In my case the directory is C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

So we want to create a share for this directory so that we can compile into it from the laptop. So create a share and make sure you have update permission to it. I created a Share called sp_bin. So configure your project to output the assembly to this share.

So click on the Build tab, and change the Output path to \\myserver\sp_bin.

Now you can Build the project. We then have to update web.config is the SharePoint directory to tell it about this assembly. One of the things we need to know to be able to do this is the value of PublicKeyToken. There are various ways of doing this. I used a tool called Reflector for .NET

So add a line like this to web.config

<SafeControl Assembly="MyFirstCSharpWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8f652744e6fb53b5" Namespace="MyFirstCSharpWebPart" TypeName="*" Safe="True" />

So now the Hello World Web Part is ready to be used by SharePoint !

Now you can add a simple site and add your custom web part to your site.