Déployer des formulaires InfoPath avec PowerShell

Comment pouvez vous utiliser InfoPath Forms Service de manière rapide tout en suivant les bonnes pratiques de déploiement des formulaires InfoPath? Pour cela, vous devez déployer des formulaires InfoPath avec PowerShell. Comme souvent, PowerShell vous facilitera rapidement et grandement la vie.

Formulaires InfoPath

Préparer vos formulaires

Premièrement, vous devez préparer votre formulaire pour qu’il puisse être ajouter dans InfoPath Forms Service. Le cas échéant vous obtiendrez le message : The XSN cannot be used on the server. Pour ajouter tester votre formulaire, vous pouvez utiliser Test-SPInfoPathFormTemplate. S’il est valide, alors vous utiliserez Install-SPInfoPathFormTemplate.

# erreur obtenue si le formulaire n'est pas valide
Install-SPInfoPathFormTemplate -Path "E:ressourcesinfopathformulaire.xsn"
Install-SPInfoPathFormTemplate : The XSN cannot be used on the server.
At line:1 char:31
+ Install-SPInfoPathFormTemplate <<<<  -Path E:ressourcesinfopathformulaire.xsn
+ CategoryInfo          : InvalidData: (Microsoft.Offic...mTemplateCmdlet:
InstallFormTemplateCmdlet) [Install-SPInfoPathFormTemplate], ConverterExce
ption
+ FullyQualifiedErrorId : Microsoft.Office.InfoPath.Server.Cmdlet.InstallF
ormTemplateCmdlet

# tester le formulaire
Test-SPInfoPathFormTemplate -Path "E:ressourcesinfopathformulaire.xsn"
This form template requires administrator approval.
To publish the form template, click the File tab, click the SharePoint Server button on the Publish tab,
and select the Administrator-approved option in the publishing wizard.

Afin de pouvoir publier votre formulaire dans InfoPath Forms Service, vous devez effectuer 2 opérations préalables dans InfoPath :

  1. Modifier le niveau de sécurité du formulaire:


Paramétrer le niveau de sécurité en Full Trust

  1. Publier le formulaire dans une bibliothèque et se laisser porter par le Wizard.


Publier le formulaire InfoPath avec la méthode adaptée

Publier et mettre à jour vos formulaires

Maintenant que votre formulaire est valide pour un déploiement, vous pouvez le déployer. Le principe reste le même que pour l’opération manuelle:  déploiement puis activation sur un ou plusieurs collections de site. La mise à jour nécessite de désinstaller, réinstaller puis mettre à jour le formulaire afin que les formulaires déjà utilisés soient mis à jour.

Install-SPInfoPathFormTemplate -Path "E:ressourcesinfopathformulaire-approved.xsn"
Enable-SPInfoPathFormTemplate -Identity "formulaire-approved.xsn" -Site "http://www.contoso.com"
Uninstall-SPInfoPathFormTemplate -Identity "formulaire-approved.xsn"
Install-SPInfoPathFormTemplate -Path "E:ressourcesinfopathformulaire-approved.xsn"
Update-SPInfoPathFormTemplate -Identity "formulaire-approved.xsn"
# google it
function WaitForJobToFinish([string]$JobName){}

$dirpck = Split-Path $MyInvocation.MyCommand.Path

function InstallOrUpdateInfoPathForm{
   param(
      $fileInfoPath,
      $webUrl
   )
   Uninstall-SPInfoPathFormTemplate -Identity $fileInfoPath -EA 0
   if($? eq $true){
     Disable-SPInfoPathFormTemplate -Identity $fileInfoPath -Site $webUrl
     "Uninstalling..."
     WaitForJobToFinish "solution-deployment-form-*"
     "Installing..."
     Install-SPInfoPathFormTemplate -Path "$dirpckressources$fileInfoPath" -Confirm:$false
     "Updating..."
     Update-SPInfoPathFormTemplate
   }else{
     "Installing..."
     Install-SPInfoPathFormTemplate -Path "$dirpckressources$fileInfoPath" -Confirm:$false
     "Enabling..."
     Enable-SPInfoPathFormTemplate -Identity $fileInfoPath -Site $webUrl
   }
}

InstallOrUpdateInfoPathForm -fileInfoPath "formulaire.xsn" -webUrl "http://www.contoso.com"

Coup de pouce : pour créer la liste contenant les formulaires par PowerShell, vous utiliserez le modèle Form Library

Fichiers de connexion

Comme vous vous en doutez, PowerShell permet aussi le déploiement des fichiers de connexion.

Install-SPDataConnectionFile Identity "connexion.udcx"
Uninstall-SPDataConnectionFile Identity "connexion.udcx"

Vous avez maintenant tout ce qu’il faut pour faire d’InfoPath un allié et vous faciliter la vie.

Références:

Install-SPInfoPathFormTemplate

Install-SPDataConnectionFile