in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Michael Hofer - SharePoint Blog

Michael Hofer's blog about adventures in SharePoint land, including tips and tricks for all products and technologies used in Information Worker solutions.

SharePoint Solution Deployment: Handy Post-Build Events

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!?

Comments

 

Sharepoint 2007 link love 07-06-2007 at Virtual Generations said:

Pingback from  Sharepoint 2007 link love 07-06-2007 at  Virtual Generations

July 6, 2007 5:51 AM
 

Michael Hofer - SharePoint Blog said:

(please read the Part 1 here ) In the second part of building a professional set of Metatags for a public

July 7, 2007 3:08 PM
 

Deepesh said:

Basically it's a question -

How would you deploy a solution which has a dependency on a class library (utility.dll) in vs solution.

Eg - I have a VS solution with a utility class library and a usermanagement sharepoint webpart.

In usermanagement sharepoint webpart, I add utility as a reference. Inside the webpart's render method I am using some of the utility class function.

Now when I do build (ctrl F5), it deploys the wsp solution to the sharepoint site, but unable to view it (some error) - basically it cannot find the utility.dll -

do you know how to make this kind of struture work?

Thanks

Deep

July 9, 2007 11:00 PM
 

mhofer1976 said:

@Deep:

I think your scenario describes more or less what I have here. Just that you also have your feature .xml that actually will deploy the webpart, I guess.

In order to deploy your utility dll, make sure that you strong name it. Now add it to the manifest file as described in my post. In the .ddf file make sure that it is included in the WSP, just like I added my DLL ("MichaelHofer.SharePoint.PublishingEnhancements.dll" <-- I suppose that the DLL is copied locally into the bin folder where you compile your webpart).

If you install your WSP now, your dll should be inserted in the GAC and it should be added to the "SafeControls" section of the web.config of each SharePoint web application where the solution is deployed.

If you have anymore questions, just let me know.

Bye Michael

July 11, 2007 2:42 PM
 

jiwanid said:

Michael,

I am not sure if I phrase it correctly, but utility.dll is not a webpart. Its a class library which has some shared code which I would like to use in my webpart without replicating the code.

If I have to manually copy my utility.dll in the bin directory, than WSP solution creation is not complete as when I give this to my packaging team, I will have to specifically mention that apart from deploying the wsp, utility dll needs to be copied to the bin directory.

Thanks

Deep

July 13, 2007 1:29 PM
 

mhofer1976 said:

@Deep:

Maybe you misunderstood me. When you compile your webpart project, use the "copy to local" option of your referenced shared dll. Like that, it is copied to the "bin" folder of your webpart project. Now you can apply the post build script to include this dll too, not only your webpart project dll. Like this, both DLLs will be deployed to the GAC when installing the solution.

Hope that makes it clearer, otherwise, drop me a mail to follow this up.

Thanks for reading my blog, bye

Michael

July 13, 2007 1:59 PM
 

jiwanid said:

Michael,

Thanks for the tip.  I will try and will let you know if it did worked or not.

Thanks for the time

Deep

July 24, 2007 11:46 AM
 

Madhu Nomula said:

Hi mhofer1976,

I created one custom workflow in my local system.

I want to deplot in server.Please list out the steps.

Thanks in advance

Madhu

August 13, 2007 6:29 AM
 

Rohan Fernando said:

This is great. It helps me to deploy workflows in to the MOSS. Thanks

February 24, 2008 9:03 PM
 

Michael said:

Actually post builds are not of any use to me, because they change the project file - and so once the project file is checked into VS - all other developers now have this post build event.....

Making post build events impractical to use in a team environment.

September 4, 2008 6:48 AM
 

PrashanthSpark said:

I have to deploy my site as WSP package.

my site has templates (customized)

webparts, virtual directory on same port.

also i have data with site...

My site is migration project from lotus notes.

please any one who can guide me. how to create using WSP packager

September 24, 2008 4:40 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About mhofer1976

For all of my IT carreer, I've been addicted to Microsoft-based software development, starting with VB, but then heading straight towards the .NET Framework in its earliest days. While working in different positions as a lead developer, project manager, pre-sales consultant and most recently a business unit manager, I've always kept beeing a developer and specialized on solutions in the Information Worker area, with an emphasis towards Enterprise Content Management. The Microsoft Office System and especially the SharePoint products and technologies are my favorite "playground", one big reason why I've joined the Microsoft Consulting Services in Switzerland where I'm currently working as Senior Consultant for Information Worker solutions.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts