This is a tutorial for deploying webparts to Sharepoint MOSS 2007 by creating a solution package and deploying using stsadm.
Assembly Info
Create your webpart in the normal way then add the following line to the AssemblyInfo.cs file
[assembly: System.Security.AllowPartiallyTrustedCallers()]
Web Part
Next, add the following above the class declaration in your web part:
[XmlRoot(Namespace = "MyWebParts")]
The value of the Namespace attribute should match the namespace of your project. Below is an example of what your web part should look like.
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 must be different from classname
namespace MyWebParts
{
[Guid("b6e6a8fb-78f2-4561-bd6f-cfbc55f07e56")]
[XmlRoot(Namespace = "MyWebParts")] // same as namespace
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
writer.Write("Output HTML");
}
}
}
Next we add two extra files to the project; a “dwp” file (web part definition file) and a manifest.xml file. Both these should be in the root directory of your project.
Web Part Definition file
Add an xml file to your project and name it whatever your project name is, in this example it would be SimpleWebPart. Give it the extension dwp. So, the final filename would be SimpleWebPart.dwp. Enter the following data into the dwp file:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>Simple Web Part</Title>
<Description>a Test</Description>
<Assembly>SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5c87de9a2116ae0a</Assembly>
<TypeName>MyWebParts.SimpleWebPart</TypeName>
</WebPart>
The Assembly element contains the classname and the TypeName element contains the namespace, classname.
Make sure the assembly is signed before compilation then use reflector to get the public key token and then correct the token in the dwp file – then compile again.
You will need to create one dwp file for each web part that exists in your project.
Manifest.xml
<?xml version="1.0"?>
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
<Assemblies>
<Assembly FileName="SimpleWebPart.dll">
<!-- Use the <ClassResource> tag to specify resources like
image files or Microsoft JScript files that your Web Parts use. -->
<!-- Note that you must use relative paths when specifying resource files. -->
<ClassResources></ClassResources>
<SafeControls>
<!-- TypeName=ClassName. One for each webpart in the solution -->
<SafeControl Namespace="MyWebParts" TypeName="SimpleWebPart" />
</SafeControls>
</Assembly>
</Assemblies>
<DwpFiles>
<!—- create one for each webpart -->
<DwpFile FileName="SimpleWebPart.dwp" />
</DwpFiles>
</WebPartManifest>
You need to create one SafeControl element for each webpart in your solution. Similarly, you will need to create a DwpFile element for each dwp you have in you project.
IMPORTANT! Make sure the project is signed, rebuild, then use reflector to get the public key token and then correct the token in the dwp file – then compile again.
CAB File project
Next create a CAB file project in the same solution as your webpart.
Right click the CAB file project and choose Add > Project Output. From the list choose Primary output. Do the same again and add Content Files. Right click Add > Files and browse to add each of the dwp files from you project.
Now build the solution.
Deploying the webpart
Now deploy the package using stsadm (which can usually be found in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN).
First open a new command prompt window and navigate to the directory, which contains the cab file you created, then run the following command
stsadm -o addwppack -filename simplewebpartcab.cab -url http://urltomoss -globalinstall –force
Finally restart IIS
That should be it! You should now be able to add your webpart to your Sharepoint pages.
Tip: Once the webpart has been deployed, you can actually copy your compiled dll into either GAC or your sharepoint bin. You can read more on the post MOSS 2007 Web Part Deployment using Visual Studio Extensions
Thanks to David Bowden for producing the initial version of the document