Installing SharePoint 2013 apps with PowerShell
Posted
Thursday, February 28, 2013 11:00 AM
by
CoreyRoth
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
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
Like always with PowerShell, no news is good news. At this point, you should find the app installed on the Site Contents pages.
Here’s what it looks like it is running. This is mainly for reference when we start talking about updates.
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" }
Once you have a reference to the App Instance, removing the app from a subsite is easy with Uninstall-SPAppInstance.
Uninstall-SPAppInstance -Identity $instance
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
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
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.