Monday, July 03, 2006

Deploying Web Parts with Solution Packages in WSS v3/MOSS 2007


Back in the good ‘ol days of WSS v2 and SPS 2003, we deployed web parts in CAB files called wppacks.  While wppacks can still be used to distrubute web parts in WSS v3 and MOSS 2007, a more compelling method is now to deploy your custom web parts along with features, site definitions and other artifacts in a SharePoint solution package.

The manifest.xml for a SharePoint solution package that includes web parts might look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="1F693B5D-87CB-4509-83E7-8681E7B032A3"
    xmlns="http://schemas.microsoft.com/sharepoint/">
 
  <FeatureManifests>
    <FeatureManifest Location="SPSolutionsDemoFeature\Feature.xml"/>
  </FeatureManifests>

  <SiteDefinitionManifests>
    <SiteDefinitionManifest Location="SPSolutionsDemo">
      <WebTempFile Location="1033\xml\webtempspsolutionsdemo.xml"/>
    </SiteDefinitionManifest>
  </SiteDefinitionManifests>
 
  <TemplateFiles>
    <TemplateFile Location="ControlTemplates\SPSolutions\SPSolutions.Demo.MyUserControlTemplate.ascx"/>
    <TemplateFile Location="ControlTemplates\SPSolutions\SPSolutions.Demo.SimpleControlTemplate.ascx"/>
    <TemplateFile Location="Layouts\ClientQueryTest.aspx"/>
  </TemplateFiles>
 
  <Assemblies>
    <Assembly DeploymentTarget="WebApplication"
          Location="SPSolutions.Demo.WebParts.dll">
      <SafeControls>
        <SafeControl Assembly="SPSolutions.Demo.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9db3057acd9c0f6"
          Namespace="SPSolutions.Demo.WebParts"
          TypeName="*" Safe="True" />
      </SafeControls>
    </Assembly>
  </Assemblies>
 
</Solution> 

Note the <SafeControl> element within <Assemblies><Assembly>.  This same <SafeControl> element will be reflected within SharePoint’s web.config upon successful installation of the solution package. 

The deployable CAB file is built using a tried-and-true utility from Microsoft called makecab.exe.  Makecab has been around since most those smart, young kids at Microsoft still had running noses and can be downloaded as part of the Microsoft Cabinet Software Development Kit.  The command to create a CAB file takes a configuration file or “DDF” as a parameter and looks something like this:

makecab.exe /f package.ddf

The contents of the DDF file tell makecab how to contruct the CAB file.  An  example DDF file might look something like this:

;*** Sample Source Code MakeCAB Directive file example
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=SPSolutionsDemoPackage.cab   
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=Package
manifest.xml
Assemblies\SPSolutions.Demo.WebParts.dll
.Set DestinationDir=ControlTemplates\SPSolutions
ControlTemplates\SPSolutions\SPSolutions.Demo.MyUserControlTemplate.ascx
ControlTemplates\SPSolutions\SPSolutions.Demo.SimpleControlTemplate.ascx
.Set DestinationDir=Layouts
Layouts\ClientQueryTest.aspx
.Set DestinationDir=SPSolutionsDemoFeature
Features\SPSolutionsDemoFeature\Feature.xml
Features\SPSolutionsDemoFeature\Elements.xml
.Set DestinationDir=SPSolutionsDemo
SiteTemplates\SPSolutionsDemo\default.aspx
SiteTemplates\SPSolutionsDemo\defaultdws.aspx
SiteTemplates\SPSolutionsDemo\ONET.xml
.Set DestinationDir=1033\xml
1033\xml\webtempspsolutionsdemo.XML
;*** <the end>

Once the redistributable cab file is created, we can use the stsadm.exe tool (located in c:\program files\common files\microsoft shared\web server extensions\12\bin\) that comes with SharePoint to actually  deploy the solution package.  Deploying a solution package is a two-step process that first involves adding the package to SharePoint’s solution store like this:

stsadm -o addsolution -filename SPSolutionsDemoPackage.cab

And then deploying the solution to a particular scope like this:

stsadm -o deploysolution -name SPSolutionsDemoPackage.cab -local -url http://beta

8 comments:

Anonymous said...

Can you plase let me know where I can find the complete schema of the file

Carlos Lopez said...

Congratulation for this post. My team has already done as you describe here, but now I'm researching the best way to avoid replace some config files (customized config files) this way each time we deploy the application, we won't rewrite those files by hand. Do you have an idea about this?

Thanks in advance.

Janakiraman C said...

Hi,

I tried to add xml and image file as
SiteDefinitionManifests
SiteDefinitionManifest Location="STS"
WebTempFile Location="1033\STS\demotemp.XML"
/SiteDefinitionManifest
/SiteDefinitionManifests

TemplateFiles
TemplateFile Location="1033\images\email.gif"/
/TemplateFiles

After creating wsp file when I try to add the wsp to SP solution store it populates an error msg "Cannot find this file specified in manifest file: 1033\STS\ demotemp.xml" I am getting same error for oth the files.

Also rectify whether license key can e done using Solution.

Guide me how to achieve this issue (ASAP)...

Anonymous said...

Awesome post, I referenced your solution pack post at the end of mine. Your the man ;)

http://www.theartofsharepoint.com/2007/05/how-to-build-solution-pack-wsp.html

Rod said...

Here's a nice easy way to do it for beginners:
http://roddotnet.blogspot.com/2007/09/deploying-wss-30-web-parts-easily-to.html

Anonymous said...

Hi hello,

My requirement is to Deploy webpart to particular site collection of web application. I have created webpart with user controls. Adding user control dll to bin of web application and webpart dll to GAC. Guide me how to deploy webpart in such a way that it should be available to parituclar site collection.

Hafees Muinudeen said...

Hi, I have developed custom web part in VS 2005 but I want to deploy it wss 2.0. How can do it?

Thanks in advance.

Haf

Anonymous said...

Hi,

Just a quick question as i am new to sharepoint can we add TemplateFiles to webpart (.cab) deployed in sharepoint server 2003 using the way you mentioned in manifest.xml.
If its not possible can you please let me know an altrenative way of icnluding them in the cab.