in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

Installing SharePoint 2013 apps with PowerShell

My PowerShell posts always prove to be the most popular.  I showed all sorts of cool PowerShell tricks in my talk back at SPC12.  One of the things I covered was how to install and manage apps using PowerShell.  Since there is a corporate catalog and SharePoint store, you might not think you need to install apps with PowerShell, but your developers may choose to the app model for a future internal project, so as an IT Pro you need to know how to install the thing.  There are a lot of similarities to working with solution packages but there are several differences to be aware of.  The documentation on TechNet is pretty good, but putting it all together can be tricky.

Specific to PowerShell, there is some terminology to familiarize yourself with.  You’ll see these for parameter names so you need to know what they are otherwise you’ll find yourself confused.

  • App Package – physical file containing the app (.app file)
  • App – an instance of an app installed on a particular subsite

Along with the App, you’ll find an Id property that refers to a GUID of that particular app instance.  We’ll talk about that more when it comes to updates.

To begin installing app, we first install the .app file into a site collection and then we deploy the app to an individual subsite.  To install the app package, we use Import-SPAppPackage.  Specify the path to your app package (.app file) with the –Path parameter.  You’ll also need to specify which Site Collection will contain the app using the –Site parameter.  Finally, the Source parameter tells SharePoint where the app came from (SharePoint Store, Corporate Catalog, or Object Model).  I always specify a value of ObjectModel for my custom apps.  Here’s what the command looks like.  Assign the results of the command to variable so that you can use it in the next step.

$spapp = Import-SPAppPackage -Path .\spcdemoapp.app -Site http://server/sitecollection –Source ObjectModel

PowerShellAppImportSPAppPackage

You can always add –confirm:$false to avoid getting prompted.

Now you have the AppPackage installed, you can deploy an instance of it to a subsite using Install-SPApp.  You’ll need to provide a URL to the subsite using the –Web parameter and a reference to the imported app package.  That’s why we saved the results of the last command into $spapp.  Use the following command.

$instance = Install-SPApp -Web http://server/sitecollection/site -Identity $spapp

PowerShellAppInstallApp

Like always with PowerShell, no news is good news.  At this point, you should find the app installed on the Site Contents pages.

NewAppInstalled

Here’s what it looks like it is running.  This is mainly for reference when we start talking about updates.

NewAppDefaultPage

To determine what apps are installed on a particular subsite use Get-SPAppInstance.  This cmdlet can be executed three different ways.  You need the id of the App Instance to update, export, or remove it.   So often you combine it with a Where-Object command to get a reference to an app instance by name instead of by id.

$instance = Get-SPAppInstance -web http://server/sitecollection/site | Where-Object { $_.Title -eq "MyApp" }

PowerShellAppGetSPAppInstance

Once you have a reference to the App Instance, removing the app from a subsite is easy with Uninstall-SPAppInstance.

Uninstall-SPAppInstance -Identity $instance

PowerShellAppUninstallSPAppInstance

One nice feature of PowerShell is that you have the ability to export an installed app to a .app file using Export-SPAppPackage.  It takes a parameter named -App.  It expects the value from an instance called .App (so use Get-SPAppInstance as shown above).  You also want to specify where to save the file using the –Path parameter.

Export-SPAppPackage –App $instance.App –Path .\ExportApp.app

PowerShellAppExportSPAppPackage

When it comes to updating apps, that’s where things get a bit tricky.  Here is what you need to do:

  • Get a reference to the existing installed instance using Get-SPAppInstance shown earlier
  • Import the new app package with Import-SPAppPackage
  • Use Update-SPAppInstance with a reference to the imported app package and the existing instance

Here are the commands

$instance = Get-SPAppInstance -web http://server/sitecollection/site | Where-Object { $_.Title -eq "MyApp" }

$spappv2 = Import-SPAppPackage -Path .\myapp.app -Site http://server/sitecollection/site -Source ObjectModel

Update-SPAppInstance – Identity $instance –App $spappv2

PowerShellAppUpdateSPAppInstance

One thing you might have noticed by now is that there is no way to retrieve which app packages are installed nor is there any way to remove them using PowerShell.  This is a bit different than the way we deal with solution packages.  I suspect it cleans itself up, but I’m really not sure.  I hope these PowerShell commands prove to be useful to you.  Let me know if you run into any issues.

Comments

 

Shilpa said:

Nice article.

March 1, 2013 3:03 AM
 

Installing SharePoint 2013 Apps | Sladescross's Blog said:

Pingback from  Installing SharePoint 2013 Apps | Sladescross's Blog

March 4, 2013 1:08 AM
 

Installing SharePoint 2013 apps with PowerShell - Corey Roth [MVP] | SharePoint Resources | Scoop.it said:

Pingback from  Installing SharePoint 2013 apps with PowerShell - Corey Roth [MVP] | SharePoint Resources | Scoop.it

March 12, 2013 12:10 PM
 

Tim Vivian said:

Great Article Corey.  Very useful information that I can use in my own automation strategy.

March 12, 2013 12:42 PM
 

VIvek said:

Can we add APPS to Pages programatically? I have a sharepoint hosted app which i have to place on my subsite as soon as the site gets created. This is a functionality i need as part of my site Provisioning. Can you please help me figure out if this task is achievable using APP model?

August 6, 2013 11:46 PM
 

Juan said:

If I try and uninstall some apps I get the following error:

"Uninstall-SPAppInstance : The App package has not been downloaded."

October 22, 2013 4:56 AM
 

Robert said:

I get an invalid data error when trying to uninstall an app.  This is at the root site collection of my web application.  What am I missing?

December 19, 2013 7:56 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems