You know how to make a web part, but do you know how to get it to your production environment in the proper way?
In a past post, I did a WSS 3.0/MOSS 2007 Web Part Tutorial that showed how to make a web part. That's all great and grand, but I did not cover how to actually get it to your production environment (truth is I didn't know how at the time). Technically, you could install VS 2008 and the extension package on your production farm, but that isn't necessarily a good idea or 'best practice' by a long shot. What you want is one nice neat file that you can take to your server and upload as a feature, that is what this tutorial is all about (most of this info was deciphered from the SharePoint 2007 SDK.)
We are going to assume that you already made your web part, and I am going to use the one from my past tutorial as the example for simplicity's sake.
Setup your Solution Structure
In order for this to compile correctly, your web part and cab projects must be inside the same solution.
- Here we go: Open Visual Studio and Choose File > New Project, once in that dialogue, click on Other Project Types > Visual Studio Solutions and choose Blank Solution. Name it something, I am going to use TestSolution for mine.
- This is a tricky part, but it is to keep the solution folder open and not go to a smaller project when we import one: In the Solution Explorer, right click on the solution and choose Add > New Solution Folder and name it dummy -- we will delete this later, but we need it for a moment.
- In the Solution Explorer, right click on the solution and choose Add > Existing Project now navigate to your existing csproj (in my case, WPs.csproj) and click OK.
- Now, in the Solution Explorer, right click on the solution and choose Add > New Project click on Other Project Types > Setup and Deployment and choose CAB Project name it and click OK (I named mine TestCab).
- Now go back and delete the dummy folder.
Now you have the proper setup and structure to your folder. Only a couple more steps and we will have a nice portable web part. Your Solution Explorer should now look like this:
Prepare your Manifast and dwp files
These are required in order for your cab to be properly set up and used by SharePoint.
- Right click on the Web Part Project inside the Solution and add an XML File, name it Manifest.xml, insert the following text into the file (notice the noly changes you might need to make are to the Namespace, which is used in your .cs file of the webpart, and FileName attributes):
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<!-- 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. -->
- Now right click on the sameProject and add another xml file, name this one your_web_part_name.dwp, for my example I am using WPs.dwp. Notice that it is the same as specified in the FileName attribute in the xml file above -- add this text to your dwp file:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>Title goes here</Title>
<Description>Put your description here.</Description>
<Assembly>WPs, Version=220.127.116.11, Culture=neutral, PublicKeyToken=9f4da00116c38ec5</Assembly>
<!-- Specify initial values for any additional base class or custom properties here. -->
The only part that may be a snag for some people is populating the <Assembly>'s PublicKeyToken and <TypeName> tags in the above XML file. Let me try and explain what and how to get these values:
- To get the <Assembly>'s PublicKeyToken you need to go to your test machine, where the code was compiled and get the PublicKeyToken - that is the value you enter there.
- As for the <TypeName> that is simply the Namespace.WebPartName of the web part itself.
- Now right-click on your CAB Project and choose Add > Project Output once in that dialogue, choose Primary Output and Content Files, Click OK.
- Right-click again on the CAB Project and Choose Add > File then browse you your dwp file you just made (mine is WPs.dwp), click Open and save your Solution.
You should now have everything set up correctly, your Solution Explorer should look like this:
Build and Deploy
All the hard work is done, now just build and deploy.
You are all done. You should have your web part available to all of your users on the farm now. And you can pass out the .CAB file to whoever wants it without any messy code to worry about!