Writing build and deployment scripts for SharePoint solutions
For medium and large SharePoint solutions it is nice to have building and deployment scripts to save a lot of time you otherwise waste on repeating same actions manually. In this posting I will show you how to create simple scripts that build, package and deploy custom SharePoint solution built on Visual Studio 2008.
Solution structure and scripts
To get scripts small and focused on conrete task we will create more than one batch file. It makes it very easy to create scripts for composite tasks – just add calls to base scripts and you are done. We will write the following scripts:
- build.bat – builds Visual Studio solution using MSBuild,
- package.bat – creates deployment packages for all projects using WSPBuilder,
- deploy.bat – deploys all solution packages to SharePoint,
- recycle-all.bat – recycles all application pools,
- replace-spsite.bat – replaces test site with new one if solution contains site template,
- full-build.bat – runs all previous scripts (you can call it also coffee-break.bat).
We also need some modifications to our Visual Studio solution. It is good practice to keep all external tools you are using in your solution also in source-code control. So we need solution folder for tools. And we also need another solution folder – we need a place for scripts too. You can see Tools and Scripts folders on the image above. This image also illustrates Visual Studio solution folder structure.
NB! Scripts provided here are very raw but they work. At least for us. There is lot of room for these scripts to be improved and optimized.
Build script
Build script will build Visual Studio solution. This is the first script we run when we start replacing our test site. I am using MSBuild because it turns out to be easiest tool for this task and also it is also in your computer if you have Visual Studio installed.
As I have currently no more actions related to code building then one line is enough from me.
Tools\MSBuild.exe Solutions\Eneta.Portal.sln
You can add all other build related commands to this file too.
Packaging script
After build we need to create new deployment packages for solutions. We will use WSPBuilder command-line tool that creates solution packages and deployment scripts. Here is the packaging script.
Tools\WSPBuilder.exe -SolutionPath Projects\Project1 -ProjectPath Projects\Project1 -OutputPath Projects\Project1 -ExpandTypes true -CreateDeploymentFolder stsadm
...
Tools\WSPBuilder.exe -SolutionPath Projects\ProjectN -ProjectPath Projects\ProjectN -OutputPath Projects\ProjectN -ExpandTypes true -CreateDeploymentFolder stsadm
Notice that WSPBuilder is put into Tools folder and called from there. Also notice that there is packaging command for each project in solution that needs packaging.
Recycle script
Well, recycling application pools can be done also on one line but it is not very sure I will use same recycling mechanism forever from now on. Also it is possible that same tools are not available on some development environments.
%windir%\system32\inetsrv\appcmd.exe list apppool /xml | %windir%\system32\inetsrv\appcmd.exe recycle apppool /in
This recycle method works well if you have IIS 7.0.
Deployment script
Deployment scrip deploys all our solutions to current SharePoint server. It is the slowest script and I don’t suggest it to run without good reason. It uses global deployment so all solutions regardless of their contents get deployed for sure. It is also compatible to deployments that WSPBuilder allows you to do through Visual Studio IDE.
Tools\gacutil.exe /u Project1
...
Tools\gacutil.exe /u ProjectN
iisreset
cd Projects\Project1\bin\deploy\Project1
call deploywithstsadmnocontenturls.bat
cd ..\..\..\..\..\
call recycle-all.bat
...
cd Projects\ProjectN\bin\deploy\ProjectN
call deploywithstsadm.bat
cd ..\..\..\..\..\
call recycle-all.bat
Before using this script we have to copy gacutil.exe to Tools folder. Before deploying solution to SharePoint the script removes all related assemblies from Global Assembly Cache (GAC). To make sure that there is no instances of assemblies anymore used by SharePoint iisreset is called.
Now take a look at two deployment call blocks shown above. First one of them makes global deployment to SharePoint. ProjectN will be deployed to all content applications automatically. It depends on your Visual Studio project contents what kind of deployment to use.
Site replacement script
Site replacement script is optional. My solution contains site definition that gets updates couple of times per day and I have to test if sites created by this definition are created correctly. Best way to test it is to create a new site. I prefer keeping things simple in my development machine so I delete site collection and create it again based on my site definition. Here is my script.
call "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" -o deletesite -url http://mossdev
call "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" -o createsite -url http://mossdev -owneremail me@example.com -ownerlogin mossdev\Administrator -lcid 1033 -sitetemplate SiteDef#0
Consider hard if you want to run this script for update deployments because this script deletes site and all its subsites and creates new site based on given site definition.
Full-run script
Our last script is for full run. It buils, packages, deploys our solution and replaces site collection. Script is simple because it calls other scripts we wrote previously.
call build.bat
call package.bat
call deploy.bat
call replace-spsite.bat
You don’t have to run these scripts for all builds because they take some time to complete. If you are modifying only some web parts and you need to get them online then do it through Visual Studio IDE.
Conclusion
As you saw it is not hard to write build and deployment scripts. You saw also that using powerful tools keeps your scripts simple. Of course, scripts provided here are just examples of what is possible. You should write your own scripts for your own scenarios.
References
Related posts: