SharePoint Pals
 | Sign In
PowerShell Script to Install SharePoint 2013 Provider Hosted Application
by Sathish Nadarajan 29 Jul 2013
SharePoint MVP
Today  :   11     Total  :    14837

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.

 [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 = "";
 #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=, 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;
     $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 "- "
     Write-Host -ForegroundColor Yellow "- An unknown error has occured during app installation. Read SharePoint log for more information.";
blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Note: For Customization and Configuration, CheckOutRecent Tweets Documentation