PowerShell Script to Install SharePoint 2013 Provider Hosted Application


Sathish Nadarajan
SharePoint MVP
Published On :   29 Jul 2013
Visit Count
Today :  1    Total :   16868
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


We had seen many articles regarding the development of Provider Hosted Application, as well as how to deploy them using the visual studio. In this article I’ll explain how to use PowerShell script to install an App.

Now let us assume that we need to move the app from environment to environment. i.e., from the Development box, we are planning to move the app to staging. At that time, you can’t expect visual studio on the Staging machine. We need to install the app through the GUI. But even that also a time consuming process and there will always be an equivalent power shell script for everything on the SharePoint.

To pack the app, always the best practice will be using the power shell script. For the installation of app, the power shell script would be as follows.

 param
 (
 [string]$Web = "https://MySiteCollection",
 
 [string]$Source = "DeveloperSite"
 )
 
 
 Write-Host -ForegroundColor White "-------------------"
 
 Write-Host -ForegroundColor White "| App Installer |"
 
 Write-Host -ForegroundColor White "-------------------"
 
 Write-Host -ForegroundColor White "- "
 
 #Global vars
 $AppPackageName = "MYSAmpleApp.app";
 
 #Loads powershell settings
 Write-Host -ForegroundColor White "- Load Powershell context.."
 
 $0 = $myInvocation.MyCommand.Definition
 
 $dp0 = [System.IO.Path]::GetDirectoryName($0)
 
 #Loads the SharePoint snapin
 
 Write-Host -ForegroundColor White "- Load SharePoint context.."
 
 $ver = $host | select version
 
 if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
 
 if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
 {
     Add-PSSnapin "Microsoft.SharePoint.PowerShell";
 }
 [void][System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 
 
 #Imports the App package
 
 Write-Host -ForegroundColor White "- Import app package '$AppPackageName'..."
 
 $appPath = $dp0 + "\" + $AppPackageName;
 
 if ($Source.Equals("ObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase)) 
 {
     $sourceApp = ([microsoft.sharepoint.administration.spappsource]::ObjectModel);
 }
 elseif ($Source.Equals("Marketplace", [System.StringComparison]::InvariantCultureIgnoreCase))
  {
     $sourceApp = ([microsoft.sharepoint.administration.spappsource]::Marketplace);
 }
 elseif ($Source.Equals("CorporateCatalog", [System.StringComparison]::InvariantCultureIgnoreCase))
 {
     $sourceApp = ([microsoft.sharepoint.administration.spappsource]::CorporateCatalog);
 }
 elseif ($Source.Equals("DeveloperSite", [System.StringComparison]::InvariantCultureIgnoreCase))
 {
     
     $sourceApp = ([microsoft.sharepoint.administration.spappsource]::DeveloperSite);
 }
 elseif ($Source.Equals("RemoteObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase))
 {
     Write-Host -ForegroundColor White "- SharePoint Developer Site.."
 
     $sourceApp = ([microsoft.sharepoint.administration.spappsource]::RemoteObjectModel);
 }
 
 $spapp = Import-SPAppPackage -Path "$appPath" -Site $Web -Source $sourceApp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
 
 
 if ($err -or ($spapp -eq $null)) 
 {
     Write-Host -ForegroundColor Yellow "- An error occured during app import !"
     throw $err;
 }
 Write-Host -ForegroundColor White "- Package imported with success."
 
 #Installs the App
 Write-Host -ForegroundColor White "- Install the APP in web site..."
 
 $app = Install-SPApp -Web $Web -Identity $spapp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
 
 if ($err -or ($app -eq $null)) 
 {
 
     Write-Host -ForegroundColor Yellow "- An error occured during app installation !"
     throw $err;
 }
 $AppName = $app.Title;
 
 Write-Host -ForegroundColor White "- App '$AppName' registered, please wait during installation..."
 
 $appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
 
 $counter = 1;
 
 $maximum = 150;
 
 $sleeptime = 2;
 
 Write-Host -ForegroundColor White "- Please wait..." -NoNewline;
 
 while (($appInstance.Status -eq ([Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Installing)) -and ($counter -lt $maximum))
 {
     Write-Host -ForegroundColor White "." -NoNewline;
     sleep $sleeptime;
     $counter++;
     $appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName} 
 }
 Write-Host -ForegroundColor White ".";
 
 if ($appInstance.Status -eq [Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Installed) 
 {
     Write-Host -ForegroundColor White "- The App was successfully installed.";
     Write-Host -ForegroundColor White $appInstance
     $appUrl = $appInstance.AppWebFullUrl;
     Write-Host -ForegroundColor White "- The App is now available at '$appUrl'.";
     Write-Host -ForegroundColor White  "- (Don't forget to add app host name in your host file if necessary...).";
     Write-Host -ForegroundColor White "- "
 }
 else
 {
     Write-Host -ForegroundColor Yellow "- An unknown error has occured during app installation. Read SharePoint log for more information.";
 }
 
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint