Friday, March 9, 2012

How to upload infopathform to sharepoint using powershell

There seems to be a hundred different ways to upload InfopathForms to Sharepoint. Some like to pack it into .wsp files, which to me does not make sense since both .xsn and .wsp is just .cab files. Powershell has some really nice commands that mirrors the .wsp and features activation for Infopath. Inspired by my previous post I will first make a setup script which will call the main upload form


$FormPath="C:\you\path\goes\here\"
$SiteUrl="http://localhost/sites/yoursite/"
$FormName="Kunderelasjoner.xsn"

PowerShell -file C:\path\InstallScripts\deployinfopathform.ps1 $SiteUrl $FormPath $FormName

cd C:\path\InstallScripts\

It gets a bit ugly with the try / catch to figure out if the form is still there, but it works. A refactoring job would be to have a more specific catch. But I could not catch the ArgumentException for some reason. The ErrorActionPreference='stop' makes sure every exception makes a stop, since powershell has exceptions that makes powershell terminate whatever it is doing and those that just prints some red text to your window.

1 $FormPath="C:\you\path\goes\here\" 2 $SiteUrl="http://localhost/sites/yoursite/" 3 $FormName="Kunderelasjoner.xsn" 4 5 PowerShell -file C:\path\InstallScripts\deployinfopathform.ps1 $SiteUrl $FormPath $FormName 6 7 cd C:\path\InstallScripts\ 8 9 It gets a bit ugly with the try / catch to figure out if the form is still there, but it works. A refactoring job would be to have a more specific catch. But I could not catch the ArgumentException for some reason. The ErrorActionPreference='stop' makes sure every exception makes a stop, since powershell has exceptions that makes powershell terminate whatever it is doing and those that just prints some red text to your window. 10 11 $SiteUrl = $args[0] 12 $FormPath = $args[1] 13 $FormName = $args[2] 14 $ErrorActionPreference='stop' 15 16 Add-PSSnapin microsoft.sharepoint.powershell 17 Set-SPInfoPathWebServiceProxy -Identity $SiteUrl -AllowWebServiceProxy $true 18 19 $AdminServiceName = "SPAdminV4" 20 $IsAdminServiceWasRunning = $true; 21 22 if ($(Get-Service $AdminServiceName).Status -eq "Stopped") 23 { 24 $IsAdminServiceWasRunning = $false; 25 Start-Service $AdminServiceName 26 } 27 28 try 29 { 30 $Form = Get-SPInfoPathFormTemplate -Identity $FormName 31 } 32 catch [Exception] 33 { 34 #Write-Host "Form not previously installed" 35 } 36 37 if ($Form -ne $null) 38 { 39 40 try 41 { 42 Disable-SPInfoPathFormTemplate $Form -site $SiteUrl 43 } 44 catch [Exception] 45 { 46 #Write-Host "Form was not activated" 47 } 48 Write-Host "Removing previous version of the form" $FormName 49 50 Uninstall-SPInfoPathFormTemplate -Identity $FormName 51 52 while ($Form -ne $null) 53 { 54 try 55 { 56 $Form = Get-SPInfoPathFormTemplate -Identity $FormName 57 Write-Host -NoNewLine "." 58 Start-Sleep -Seconds 1 59 } 60 catch [Exception] 61 { 62 Write-Host "" 63 Write-Host "- Done -" 64 $Form = $null 65 } 66 } 67 } 68 69 Write-Host "Install form" $FormName 70 71 $form = Install-SPInfoPathFormTemplate -Path ($FormPath + $FormName) -confirm:$false 72 73 while ($form.FormTemplateStatus -ne "Normal") 74 { 75 Write-Host -NoNewLine "." 76 Start-Sleep -Seconds 1 77 } 78 Write-Host "- Done -" 79 80 Write-Host "Activating form " $FormName 81 82 Enable-SPInfoPathFormTemplate -Identity $FormName -Site $SiteUrl 83 84 Write-Host "- Done -" 85 86 if (-not $IsAdminServiceWasRunning) 87 { 88 Stop-Service $AdminServiceName 89 }

Wednesday, March 7, 2012

How to deploy to sharepoint using powershell

In my first blog post I will have closer look at how to deploy .wsp to Sharepoint 2010 using a powershell script. My inspiration has been these gentlemen
  • http://sharepintblog.com/2011/06/04/exporting-solutions-packages-wsp-with-powershell/
  • http://www.projectserver2010blog.com/2010/01/sharepoint-2010-solution-deployment.html
  • https://nickhobbs.wordpress.com/2012/02/24/sharepoint-2010-powershell-to-wait-for-wsp-solution-deployment-timer-job-to-complete/

So first up I have an setup file with that includes the properties that is specific for a solution and the feature that it contains. Which the same as i sharepintblog


$DeploymentPackageFolder="C:\slv\Main\Samhandling\SLV.Samh\SLV.Samh.KReg.SL.SP.DataConnLib\bin\Debug"

$SiteUrl="http://localhost/sites/kunderegisteret/config"
$SolutionFolder="./"
$SolutionName="DataConnLib.wsp" #constant
$FeatureName="DataConnLibFeature" #constant

cd $DeploymentPackageFolder

PowerShell -file C:\your\path\deploywithfeatureactivation.ps1 $SiteUrl $SolutionFolder $SolutionName $FeatureName

cd C C:\your\path\InstallScripts\

Then Ive added the wait function to the deploy script as I didnt think the simple while loop seemed to do the trick

function WaitForSPSolutionJobToComplete([string]$solutionName)
{
$solution = Get-SPSolution -Identity $solutionName -ErrorAction SilentlyContinue

if ($solution)
{
if ($solution.JobExists)
{
Write-Host -NoNewLine "Waiting for timer job to complete for solution '$solutionName'."
}

# Check if there is a timer job still associated with this solution and wait until it has finished
while ($solution.JobExists)
{
$jobStatus = $solution.JobStatus

# If the timer job succeeded then proceed
if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Succeeded)
{
Write-Host "Solution '$solutionName' timer job suceeded"
return $true
}

# If the timer job failed or was aborted then fail
if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Aborted -or
$jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Failed)
{
Write-Host "Solution '$solutionName' has timer job status '$jobStatus'."
return $false
}

# Otherwise wait for the timer job to finish
Write-Host -NoNewLine "."
Sleep 1
}

# Write a new line to the end of the '.....'
Write-Host
}

return $true
}

$SiteUrl = $args[0]
$SolutionFolder = $args[1]
$SolutionName = $args[2]
$FeatureName = $args[3]

$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

Add-PSSnapin microsoft.sharepoint.powershell

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
$IsAdminServiceWasRunning = $false;
Start-Service $AdminServiceName
}

Write-Host "Analyzing feature: $FeatureName"
if ((Get-SPFeature | ? {($_.Name -eq $FeatureName)}) -ne $null)
{
Write-Host "Deactivating feature: $FeatureName" -NoNewline

# Synchronous call
Disable-SPFeature $FeatureName -Url $SiteUrl -Confirm:$false

Write-Host " - Done."
}

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}
if ($Solution -ne $null)
{
Write-Host "Really rectracting solution: $SolutionName"
Uninstall-SPSolution $SolutionName -Confirm:$false

WaitForSPSolutionJobToComplete($SolutionName)

Write-Host " - Done."

Write-Host "Removing solution: $SolutionName"

# Sychrouous call - no need to wait for it
Remove-SPSolution $SolutionName -Confirm:$false

Write-Host " - Done."
}


Write-Host "Adding solution: $SolutionName" -NoNewline
$SolutionPath = $SolutionFolder + $SolutionName
Add-SPSolution $SolutionPath # | Out-Null
Write-Host " - Done."

#Paranoia, just for fun
WaitForSPSolutionJobToComplete($SolutionName)

Write-Host "Deploying solution: $SolutionName"
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}
Install-SPSolution $SolutionName -GACDeployment -Confirm:$false

WaitForSPSolutionJobToComplete($SolutionName)
Write-Host " - Done."

# Could have been enabled by the install. Must check. Darn.
if ( $FeatureName -ne $null )
{
Write-Host "Activate feature: $FeatureName" -NoNewline
Enable-SPFeature $FeatureName -Url $SiteUrl -force
Write-Host " - Done."
}

WaitForSPSolutionJobToComplete($SolutionName)


if (-not $IsAdminServiceWasRunning)
{
Stop-Service $AdminServiceName
}