Deployment of wsp solutions to a productive SharePoint farm is still a complex task (although since Visual Studio 2010 deployment steps for local deployment). But with the SPSF SharePoint Software Factory (free download at http://spsf.codeplex.com) the automated deployment of wsp solutions becomes very easy.

With SPSF developers can create SharePoint projects in Visual Studio and a separate project for deployment of the solution files is added automatically. The project “ApplicationDeployment” provides msbuild files which allow the deployment of SharePoint solutions to a SharePoint farm per script.

Today I want to describe, how Powershell scripts (.ps1) can be executed during the deployment process in SPSF. This can be useful e.g. to activate several features, to create search properties or anything else which can be done with Powershell.

For testing I used a simple script and called it Script.ps1

[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
$site=Get-SPSite “http://demo2010a”
write-output “Log message: Output of Powershell”
write-output $site.Url

This script simply requests a SPSite and outputs the URL.

1. Step: Add Powershell script to the SharePoint project in Visual Studio

The .ps1 files should be added to the project “ApplicationDeployment” in folder “DeploymentFiles”. Each file in this folder will be copied to the installation package during build. I would recommend to create a subfolder “PowerShell” to store all .ps1 files.


2. Add call to .ps1 file to the “DeploymentTargets.msbuild”

The project “ApplicationDeployment” contains a file called “DeploymentTargets.msbuild”. This file contains several targets which are automatically called during the deployment process. There are “BeforeDeploy” and “AfterDeploy“, “BeforeUpgrade” and “AfterUpgrade” and “BeforeUndeploy” and “AfterUndeploy“. These targets can contain calls to stsadm or powershell to execute actions at a special point during deployment. They can be used to activate and deactivate features, create sites etc.

To run a .ps1 Powershell script you only need to add the following statement to a target.

<!– use this target to perform operations after a successful deployment –>
<Target Name=”AfterDeploy”>
<!– call to custom .ps1 file which should be execute after deployment –>
<Exec Command=”$(PS) -file &quot;Powershell\Script.ps1&quot;” />
</Target>

The call to EXEC in msbuild runs a command line program, in this case it is Powershell.

Why should you use this approach?

The created package for the deployment writes all output of the deployment to a single logfile (Deployment.log). This is important to trace errors and warnings during a deployment.

The advantage of calling .ps1 files with msbuild is that the output of the Powershell script is written to the same lofile of msbuild and no separate logfiles must be written in the code of .ps1 files. It makes it easier to check the deployment for errors and warnings.

Alternative

You can also execute Powershell commands directly in the DeploymentTargets.msbuild. Here are some samples:

<Target Name=”AfterDeploy”><!– Sample STSADM commands
<Exec Command=”&quot;$(STSADM)&quot; -o activatefeature -name ‘Feature1′ -url ‘http://demo201a’” /><!– Sample PowerShell commands
<Exec Command=”$(PS) -command &quot;. New-SPSite -Url ‘http://demo201a’ -OwnerAlias ‘CONTOSO\Administrator’ -Name ‘Test Site’ -Template ‘STS#0′&quot;” />

<Exec Command=”$(PS) -command &quot;. Install-SPFeature -Path ‘Feature1′ -Force&quot;” />

<Exec Command=”$(PS) -command &quot;. Enable-SPFeature -Identity ‘Feature1′ -Url ‘http://demo201a’ -Force&quot;” />

</Target>

Tags: ,

In Ireland we will be at Dublin (06.03.) and Belfast (07.03.). I hope we will have again a lot of attendees and interesting discussions about development with SharePoint and SPALM.
Event infos Dublin
Event infos Belfast

At 17.03. I will be as speaker at the Microsoft TechDays in Helsinki. I will speak again about SharePoint development and SPALM. Looking forward for an interesting event. I hope I can attend some other sessions.
Microsoft Techdays Finland

Again my colleague Matthias Einig was presenting SharePoint development and SPALM at the roadshow with Microsoft. Who can read Norwegian, here you find more information.

After improving a lot we now could release our SharePoint Software Factory to the public. The software factory is an extension for Visual Studio 2010 and 2008 and makes the development for SharePoint 2010 and 2007 much more easier. Download for free at Codeplex: http://spsf.codeplex.com

Unfortunately I could not present SharePoint ALM in Sweden at 17.03.2011. But my colleague Matthias Einig did the job. See a photo of him at Steria Sweden website: Steria.se
More information from Microsoft (Swedish)

Next station for the SharePoint roadshow: Vienna. I had again the presentation about SharePoint development and SharePoint ALM. More than 80 people, great location at Microsoft Austria, great event. More information on Codefest.at

Microsoft is conducting several events in 2011 for SharePoint development. At 25.02.2011 I was part of MSDN Tech Talk. Find the presentation of the event here: Download presentations

Unfortunately we had only half a day time for the TechTalk about SharePoint development in Wallisellen near Zurich. I hope we will have some follow-up presentations to go deeper into the topic.
Event information

Directly after the first roadshow event in Madrid now Lisbon follows. Again we have more than 100 attendees and the feedback was great.
More information about the event (Spain)

Blogger writing about the event (English)

« Older entries