Having built some nice additional SharePoint WebControls, I want them to be deployed as a SharePoint solution, packed into a WSP file - and of course automated. I was very impressed how easy it is, to deploy Workflows based on the MOSS SDK Visual Studio Templates for Workflows. They use an own file structue in the project ("\DeploymentFiles") which consists of the needed .xml & .ddf files and also a .bat file that is used in a post-build command. This .bat is very flexible and does all the magic to deploy the solution and any features contained in it (that is the workflow here).
It is very easy to adopt this to any SharePoint solution. In my case, I just need to deploy a DLL into the GAC and mark it as safe control. However, it is a piece of cake to adopt the files to deploy any kind of feature too. Also, I want to deploy only when I build in "Release" mode, and only if in the post-build command I've set the "DEPLOY" option - since my post-build script performs also an IISRESET which costs a lot of time and resources...
OK, here we go:
Create a sub-folder in the project called "DeploymentFiles".
Add the "manifest.xml" file. This is very simple in my case:
<?xml version="1.0" encoding="utf-8" ?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="{91320A34-AC12-4ffa-8076-2ECFBD7BA686}" >
<Assemblies>
<Assembly DeploymentTarget="GlobalAssemblyCache" Location="MichaelHofer.SharePoint.PublishingEnhancements.dll">
<SafeControls>
<SafeControl Assembly="MichaelHofer.SharePoint.PublishingEnhancements, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ce575c89ea427a4" Namespace="MichaelHofer.SharePoint.PublishingEnhancements" TypeName="*" Safe="True" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution>
Add the "wsp_structure.ddf" file:
;This file is for WSP CAB Generation
;A WSS or in this case MOSS solution file is essentially a .cab file,
;use the makecab.exe tool to create the solution package.
;The makecab.exe tool takes a pointer to a .ddf file,
;which describes the structure of the .cab file.
;The format of a .ddf file is basically that
;you declare a standard header and
;then enumerate, one file per line, the set of files by where they live on disk,
;separated by where they should live in the .cab file
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate="MichaelHofer.SharePoint.PublishingEnhancements.wsp"
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP;** All files are compressed in cabinet files
.Set UniqueFiles="ON"
.Set Cabinet=on
.Set DiskDirectory1="Package"
;All file reference should be from the project root
;Files to place into the CAB Root
..\..\DeploymentFiles\manifest.xml
MichaelHofer.SharePoint.PublishingEnhancements.dll
Add the "PostBuildActions.bat" file. This is a cut-together version of the that is used with workflows:
@ECHO OFF
:: -----------------------------------------------
::
:: --------------------------
:: Post build actions (VS):
:: --------------------------
:: This batch file is run by VS after the user builds the project (F6)
:: It installs a simple solution package to sharepoint. No feature is installed or activated.
:: Adapted by Michael Hofer, originally, this script is used with SharePoint Workflow VS Templates.
:: By default, it skips all deployment steps so that users can build without deploying.
:: Users can use the DEPLOY parameter to install the assembly the GAC and activate
:: stsadm deployment commands.
:: --------------------------
:: How to deploy:
:: --------------------------
:: 1. Sign the assembly
:: 2. Use "Release" mode for compilation
:: 3. Change "NODEPLOY" to "DEPLOY" in the post build events (in project properties,
:: see "Build Events"->"Post-build event command line")
:: 4. Make sure that the web application urls are set correctly (Ln 88 & 96)
:: 5. Press F6 or go to Build->(Re)Build Solution
:: --------------------------
:: Usage:
:: --------------------------
:: - Deployment disabled: call "$(ProjectDir)\DeploymentFiles\PostBuildActions.bat" "$(ConfigurationName)" "$(ProjectDir)" "$(ProjectName)" "$(TargetDir)" "$(TargetName)" NODEPLOY
:: - Deployment enabled: call "$(ProjectDir)\DeploymentFiles\PostBuildActions.bat" "$(ConfigurationName)" "$(ProjectDir)" "$(ProjectName)" "$(TargetDir)" "$(TargetName)" DEPLOY
:: -----------------------------------------------
::
ECHO.
ECHO Running post build actions.
ECHO.
:: e.g."Debug" or "Release"
SET CONFIG=%1
SET CONFIG=%CONFIG:~1,-1%
ECHO CONFIG: %CONFIG%
:: constant placeholder for branch logic comparison; always "DEPLOY"
SET CONFIGVALUE=Release
IF NOT %CONFIG%==%CONFIGVALUE% (ECHO Not in Release mode, skipping deployment & GOTO QUIT)
:: e.g. "C:\Projects\MySolution"
SET PROJECTDIR=%2
SET PROJECTDIR=%PROJECTDIR:~1,-2%
ECHO PROJECTDIR: %PROJECTDIR%
:: e.g. "C:\Projects\MySolution\DeploymentFiles"
SET DEPLOYMENTDIR=%PROJECTDIR%\DeploymentFiles
ECHO DEPLOYMENTDIR: %DEPLOYMENTDIR%
:: e.g. "MySolution"
SET PROJECTNAME=%3
SET PROJECTNAME=%PROJECTNAME:~1,-1%
ECHO PROJECTNAME: %PROJECTNAME%
:: e.g. "C:\Projects\MySolution\bin\Debug"
SET TARGETDIR=%4
SET TARGETDIR=%TARGETDIR:~1,-2%
ECHO TARGETDIR: %TARGETDIR%
:: e.g. "MySolution"(.dll)
SET TARGETNAME=%5
SET TARGETNAME=%TARGETNAME:~1,-1%
ECHO TARGETNAME: %TARGETNAME%
:: cmd parameter, e.g. "DEPLOY" or "NODEPLOY"
SET DEPLOY=%6
ECHO DEPLOY: %DEPLOY%
:: constant placeholder for branch logic comparison; always "DEPLOY"
SET DEPLOYVALUE=DEPLOY
ECHO DEPLOYVALUE: %DEPLOYVALUE%
IF NOT %DEPLOY%==%DEPLOYVALUE% (ECHO Skipping deployment & GOTO QUIT)
IF EXIST "%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE" (SET STSADM="%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE") ELSE (ECHO STSAMD.EXE could not be found! & GOTO QUIT)
ECHO.
ECHO Generating %PROJECTNAME%.wsp ...
ECHO.
makecab /f "%DEPLOYMENTDIR%\wsp_structure.ddf"
ECHO.
ECHO Retracting and deleting solution (if it exists)...
ECHO.
%STSADM% -o retractsolution -name %PROJECTNAME%.wsp -url http://localhost:8081 -local
%STSADM% -o deletesolution -name %PROJECTNAME%.wsp
ECHO.
ECHO Adding and deploying the solution...
ECHO.
%STSADM% -o addsolution -filename "Package\%PROJECTNAME%.wsp"
%STSADM% -o deploysolution -name %PROJECTNAME%.wsp -url http://localhost:8081 -local -allowGacDeployment -force
GOTO FINISH
:: -----------------------------------------------
::
:FINISH
::
ECHO Doing an iisreset...
ECHO.
CALL iisreset
:: -----------------------------------------------
:: -----------------------------------------------
::
:QUIT
ECHO.
ECHO Done
ECHO.
::
:: -----------------------------------------------
Add the post-build event command (use NODEPLOY and the script doesn't execute):
call "$(ProjectDir)\DeploymentFiles\PostBuildActions.bat" "$(ConfigurationName)" "$(ProjectDir)" "$(ProjectName)" "$(TargetDir)" "$(TargetName)" DEPLOY
Switching now to "Release" build mode and press F6, the project is compiled, the WSP is generated and the solution is installed automatically. Handy, right!?