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

Post a Comment