Deploying a WebPart Solution in SharePoint 2007 the simple way
May 31, 2007Deploying web parts into MOSS 2007 isn’t exactly straight forward and after looking around the web for a while it became apparent that there is no ’standard’ way for deploying them. I looked at a few different options and found that creating a Solution file using Visual Studio’s CAB Setup Project was the easiest and most reusuable way of accomplishing this task.
Below is a step by step guide to developing and deploying a simple WebPart.
First we need to create a WebPart to deploy, the easiest way I have found of doing this is to download the Visual Studio SharePoint extensions from Microsoft which can be located here.
After installing the extensions open Visual Studio and create a new project. You should now have some extra project templates under the SharePoint section.

Select ‘Web Part’ project, give it a name and click OK.
Visual studio will now create a basic Web Part class for you to edit.
My Web Part was called ClientViewerWebPart and I inserted some code into the overridden Render method which basically outputs data from a SharePoint list. This code looks like this so far:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace ClientViewerWebPart
{
[Guid("6bf05a67-118d-4cc4-80f7-b923be02773a")]
public class ClientViewerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public ClientViewerWebPart()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
SPSite site = new SPSite(“http://leesbs/”);
SPWeb web = site.OpenWeb();
SPListCollection collection = web.Lists;
SPList list = collection.GetList(new Guid(“{936518FF-CC2E-4BD9-ABB9-0580EA04BCD6}”), false);
for (int index = 0; index < list.Items.Count; index++)
{
SPListItem item = list.Items[index];
writer.Write(“<table>”);
writer.Write(“<tr><td>Client Name</td>”);
writer.Write(“<td>” + item["Client Name"].ToString() + “</td></tr>”);
writer.Write(“</table>”);
}
}
}
}
Next we need to tell SharePoint to allow this web part to be executed from a partially trusted location. We do this by making an entry into the web parts AssemblyInfo file.
In Solution Explorer Expand Properties and open up the AssemblyInfo.cs file. At the bottom of the file insert the following:
[assembly: System.Security.AllowPartiallyTrustedCallers()]
Now we need to add a manifest file to out Web Part project. This manifest file is what defines our solution and tells SharePoint everything it needs to know about our Web Part.
Add a new XML file to tthe project and rename it manifest.xml. Insert the following XML into the file:
<?xml version=“1.0“ encoding=“utf-8“ ?>
<Solution xmlns=“http://schemas.microsoft.com/sharepoint/“
SolutionId=“{E3CF88A3-0EC3-49b9-B09E-5F84417EC6ED}“>
<Assemblies>
<Assembly DeploymentTarget=“WebApplication“
Location=“ClientViewerWebPart.dll“>
<SafeControls>
<SafeControl Assembly=“ClientViewerWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3858ebd08dca7ee0“
Namespace=“ClientViewerWebPart“ TypeName=“*“/>
</SafeControls>
</Assembly>
</Assemblies>
</Solution>
You need to provide your own GUID value for the SolutionId attribute, you can do this within Visual Studio by using the GUID Generator under the Tools menu. You also need to enter your assemblies PublicKeyToken value which can be found by either using ILDASM.exe or Reflector.
The last thing we need to do to our Web Part project is make sure it has a strong name when it’s compiled, you can do this either using the SN.exe command line tool, or opening the project properties from within Visual Studio and navigating to the Signing section. From here you can create a new key file which will be used to strong name the assembly at compile time.
Now we have a fully working Web Part which can be compiled, what we need to do now is deploy this Web Part into our SharePoint site.
We are going to use a Visual Studio setup project to accomplish this, so add a new project to the same solution your Web part project is in, and create a CAB setup project. I called my setup project ClientViewerWebPartSetup.
Right click on the project and goto Add -> Project Output, from the ‘Add Project Output Group’ dialog box, select your Web Part project and then select ‘Primary Output’.
Repeat the step above but this time instead of selecting ‘Primary Output’ select ‘Content Files’.
After this is done you should have a solution that looks something like this:

Now you can compile your setup project, this will create a cab file containing the WebPart assembly and the manifest.xml file so all we need to do now to create our SharePoint solution file is rename our .cab file to .wsp.
We are now ready to deploy this solution file into SharePoint and we do this by using the STSADM.exe tool. This tool is located in your SharePoint installation directory under the bin folder mine was located here C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN. I strongly suggest you add this path to you PATH environment variable for ease of use.
You need to use the addsolution argument to the STSADM tool the first time you deploy a Web Part into your SharePoint site. When updating the Web Part you can use the upgradesolution argument.
The following is how I deployed my Web Part into my SharePoint site for the first time.

Great! Your Web Part is now deployed into your SharePoint site, or is it?
We need to do one more thing to make out Web Part available to our SharePoint site.
Open up Central Administration and navigate to ‘Operations’ under ‘Global Configuration’ click ‘Solution Management’. You should see your Web Part Solution sitting in the list with ‘Not Deployed’ as it’s status. Click on the Solution and click the ‘Deploy Solution’ button.
You should now be able to add your Web Part to any sites Web Part gallery.
Hope this guide has been useful, it’s quite a lengthly process do first time, but should get easier the more times you do it.

June 5, 2007 at 8:01 am
[...] Deploying a WebPart Solution in SharePoint 2007 the simple way - a step by step guide to developing and deploying a simple WebPart. [...]
July 22, 2007 at 2:02 pm
Thank you very much for the information. It clarify a couple of things for me
July 22, 2007 at 2:08 pm
Glad I could help
July 26, 2007 at 12:44 pm
Dude thanks for the info, that sure did help me
July 31, 2007 at 9:26 am
good one.it helped me a lot.
July 31, 2007 at 5:06 pm
Thanks! Awesome explaination for installing a web part! I definitely think this will be useful when I get my pre-built web part installed
August 3, 2007 at 12:32 pm
Hey, what if I want to add more than one webpart together in one solution? Do I have to follow the same rules as above and just add the required cs files for the webParts and add an entry for each one in the manifest.xml?
August 18, 2007 at 1:07 am
Thanks for the great article. I am able to deploy the web part successfully, however, I get the following error when I add it to any page.
The “ProgramFilter” Web Part appears to be causing a problem. Request for the permission of type ‘Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ failed.
I can fix this problem in my DEV environment by setting the trust level in the web.config to WSS_Medium. The live server is unfortunately is shared hosting. I have a funny feeling they won’t be changing the trust level for me.
How should i proceed?
August 21, 2007 at 10:00 am
Its awesome
November 21, 2007 at 3:05 am
Thanks for the great article. Just one question, how do you go about upgrading your solution? If I update the version number in the assembly and manifest and then upgrade using ‘upgradesolution’ then all the exiting webparts in my pages fall over and have to be manually replaced with the new version. Can this be done automatically?
December 7, 2007 at 6:39 pm
I’m having trouble getting this setup, the Visual Studio SharePoint extensions wont install without WSS 3.0 installed… I dont have WSS3.0 installed on my workstation and dont really want to, is there any way around this?
December 11, 2007 at 3:29 am
Hi,
I’m trying to develop a webpart with a GridView(SPGridview ) which displays information retrieved from a list.There is also a template field containing checkboxes to select the Grid’s rows.Can anyone tell me how the rows can be selected and the values in each column retrieved to perform such operations such as edit/delete.
Thanx in advance
January 9, 2008 at 10:52 am
I am getting this error
Object Reference not set to an instance of an object
The solution installtion failed.
Please anyone help me in this matter
February 1, 2008 at 2:47 am
[...] Package & Deploy a Web Part as a Solution [...]
February 14, 2008 at 1:12 pm
Its fabulous post….. chanceless………
I need to clarify one thing how to add user controls(.ascx,.. files) in the .wsp file
February 23, 2008 at 10:01 pm
If you add this
ren $(BuiltOuputPath) *.wsp
del *.cab
to the post build events you don’t have to rename it manually
Great Article anyway
Greetz
Stefan
February 26, 2008 at 10:23 pm
Thanks. This helped!
March 25, 2008 at 7:10 am
I am getting an error:Object reference not set to an instance of an object.
When i try to complie the solution. How do i over come it?