loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Packaging and Deploying a SharePoint 2007 Web Part

by naspinski 9/1/2008 7:47:00 AM

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):

<?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="WPs.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>
                <SafeControl
                  Namespace="WPs"
                  TypeName="*"
                />
            </SafeControls>
        </Assembly>
    </Assemblies>
    <DwpFiles>
        <DwpFile FileName="WPs.dwp"/>
    </DwpFiles>
</WebPartManifest>

 

  •  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=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5</Assembly>
    <TypeName>WPs.Test</TypeName>
    <!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>

 

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.

 

  • Right click on the solution and choose Build Solution - this makes your portable web part, just look in your solution folder, in my example it is located at TestSolution\TestCab\Debug\TestCab.CAB - if you will notice, that is just: Solution_Name\Cab_Project_Name\Debug\CabFile.CAB.
  • Now that you have your file, copy it to your production environment to the folder: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin\
  • Then run the command (from that directory):

     

    stsadm -o addwppack -filename Time_Zones.CAB -url http://your_url_here -globalinstall -force

     

  • And run an iisreset.


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!

Related posts

Comments

10/9/2008 11:41:40 AM

rodrigue h
Does anyone know how to build connections between web parts?

rodrigue h fr

12/10/2008 8:50:30 PM

harsh
can we not use the tool called stsdev developed by Ted Pattison? which would generate the manifest file and the dwp file.

harsh us

7/7/2009 6:17:01 AM

SEO
Nice article.The code works fine and is a good starting point for me making a custom module.Thanks for the module..

SEO us

8/12/2009 7:28:43 AM

StoneyK
I do the same think like your instruction. but I get a error when review webpart

A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe.

someone help me

StoneyK us

11/2/2010 10:50:52 AM

pingback
Pingback from blog.zam.web.tr

howto deploy webparts to sp2007 / sp2010 farm « Alen Ziver Malhasoglu's Microsoft Commerce Server Blog

blog.zam.web.tr

9/14/2011 12:04:10 AM

pingback
Pingback from pengzhang.ca

Bookmarks for September 5th through September 13th | Peng's Blog

pengzhang.ca

7/5/2012 10:17:00 AM

pingback
Pingback from asplite.wordpress.com

Deploying custom webpart in sharepoint 2007 « asp.net snacks

asplite.wordpress.com

12/6/2012 8:58:25 PM

pingback
Pingback from asphive.wordpress.com

Deploying custom webparts in SharePoint 2007 | asphive

asphive.wordpress.com


Comments are closed