Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In SharePoint 2013 Preview, you can now use a new development model, called "App". The App model provides new way to develop (100% client side) and new process to deploy custom code on a SharePoint site.
If you want to have more information about the App Model, you can consult the following links:
As all SharePoint development models, you can deploy app with PowerShell. You can find a complete index of app PowerShell commands here.
In this article, I propose 3 simple scripts to install, update or uninstall an APP for SharePoint OnPremise. Please note that in this scripts, I assumed that :
- The app is a 100% SharePoint hosted app
- The app is distributed as a app package (*.app file), contained in the same folder
- The app package or app name are not scripts parameters, but include as scripts var
Note that you can download it as a zip archive at the bottom of the page.
PowerShell script to install an APP
param
(
[string]$Web = $(throw '- Need a SharePoint web site URL (e.g. "https://mysp15site/")'),
[string]$Source = "ObjectModel"
)
Write-Host -ForegroundColor White "-------------------"
Write-Host -ForegroundColor White "| App Installer |"
Write-Host -ForegroundColor White "-------------------"
Write-Host -ForegroundColor White "- "
#Global vars
$AppPackageName = "MyCustomAppPackage.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)) {
$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.";
$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.";
}
PowerShell script to uninstall an APP
param
(
[string]$Web = $(throw '- Need a SharePoint web site URL (e.g. "https://mysp15site/")')
)
Write-Host -ForegroundColor White "---------------------"
Write-Host -ForegroundColor White "| App UnInstaller |"
Write-Host -ForegroundColor White "---------------------"
Write-Host -ForegroundColor White "- "
#Global vars
$AppName = "My Custom App";
#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")
#Checks if the app instance exists
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
if ($appInstance -ne $null)
{
Write-Host -ForegroundColor White "- App instance detected on target web, uninstalling it...";
Uninstall-SPAppInstance –Identity $appInstance -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err)
{
Write-Host -ForegroundColor White "- An error occured during app uninstallation !";
throw $err;
}
Write-Host -ForegroundColor White "- Please wait..." -NoNewline;
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
$counter = 1;
$maximum = 50;
$sleeptime = 2;
while (($appInstance -ne $null) -and ($appInstance.Status -eq ([Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Uninstalling)) -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 ".";
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName}
if ($appInstance -eq $null)
{
Write-Host -ForegroundColor White "- App '$AppName' was successfully uninstalled.";
Write-Host -ForegroundColor White "- "
}
else
{
Write-Host -ForegroundColor Yellow "- App '$AppName' was not successfully uninstalled.";
}
}
else
{
Write-Host -ForegroundColor Yellow "- App '$AppName' not found in web site '$Web'.";
}
PowerShell script to update an APP
param
(
[string]$Web = $(throw '- Need a SharePoint web site URL (e.g. "https://mysp15site/")'),
[string]$Source = "ObjectModel"
)
Write-Host -ForegroundColor White "------------------"
Write-Host -ForegroundColor White "| App Upgrader |"
Write-Host -ForegroundColor White "------------------"
Write-Host -ForegroundColor White "- "
#Global vars
$AppPackageName = "MyCustomAppPackage.app";
$AppName = "My Custom 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)) {
$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 "- Updating the APP in web site..."
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
$app = Update-SPAppInstance -Identity $appInstance -App $spapp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err) {
Write-Host -ForegroundColor Yellow "- An error occured during app update !"
throw $err;
}
$AppName = $app.Title;
Write-Host -ForegroundColor White "- App registered, please wait during update..."
$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]::Upgrading)) -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 upgrade.";
$appUrl = $appInstance.AppWebFullUrl;
Write-Host -ForegroundColor White "- The App is available at '$appUrl'.";
Write-Host -ForegroundColor White "- "
}
else {
Write-Host -ForegroundColor Yellow "- An unknown error has occured during app update. Read SharePoint log for more information.";
}
Comments
Anonymous
June 11, 2013
Really Nice post! Please let me know if we can add an app part to a webpart page using powershell. Thanks in advance!Anonymous
December 11, 2013
Really Nice. I am trying to install Corporate News App using PowerShell but i am getting error. I downloaded Corporatenewsapp.app from Codeplex and copied that to Server hosting SP2013 i tried to run the .ps1 file after making changes to package name. Please help Thanks haryAnonymous
January 25, 2014
Thank you so much. These scripts are really helpful ! Saved us lot of timeAnonymous
March 31, 2014
Thank you so much Oliver ! Any idea on app installs/upgrades/un-install's using power shell for Online version ? Not sure if Microsoft has started supporting the "App" operations for Office 365 - Sharepoint online. Please let me know. Thanks, YatheenAnonymous
March 31, 2014
Find this interesting blog on Online version: blogs.realdolmen.com/.../managing-sharepoint-online-with-powershellAnonymous
March 29, 2015
Can we use above powershell script for provider-hosted apps?Anonymous
March 04, 2016
seems the link is not there anymoe? blogs.realdolmen.com/.../managing-sharepoint-online-with-powershellAnonymous
March 04, 2016
link is broken? blogs.realdolmen.com/.../managing-sharepoint-online-with-powershell