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 }

No comments:

Post a Comment