Yes/No Column PnP PowerShell

PnP PowerShell: How to manage an app in SharePoint?

In this article, we will look at the instructions to add/install SharePoint app using PnP PowerShell. Please note that here we are talking about the custom apps (.sppkg file) which are developed to extend the SharePoint functionalities to meet the business requirements.

Prerequisites

You need to install PnP PowerShell Module in your local machine to run the cmdlets mentioned in this article. You can visit another article written by me which is available at https://www.hemantkabra.in/microsoft-sharepoint/pnp-powershell-how-to-get-started/ to setup the PnP PowerShell.

Installation of SharePoint App

Follow below mentioned steps to install a SharePoint App

STEP 1: ADD THE SHAREPOINT APP PACKAGE

Before installation, the first step is to add the package so that it is available in the site collection for installation. We can add the package either at the tenant level or site collection level. This depends on the requirement if the SharePoint app needs to be made available in all the site collection or to a specific site collection only.

Use below mention code to add the package at tenant level:

# Url of the SharePoint Tenant
$TenantUrl = "https://hemantkabra.sharepoint.com" 

# Connect to the SharePoint Tenant
Connect-PnPOnline -Url $TenantUrl -Interactive

# Path of the App Package
$AppPath = "./myapp.sppkg"
$App = Add-PnPApp -Path $AppPath -Overwrite -Publish

# Disconnect the PnP Connection
Disconnect-PnPOnline

and use below mention code to add the package at site collection level:

# Url of the SharePoint Online Site Collection 
$SiteUrl = "https://hemantkabra.sharepoint.com/sites/mysite" 

# Connect to the SharePoint Online Site Collection
Write-Host "Connecting to Site Collection..!"
Connect-PnPOnline -Url $SiteUrl -Interactive

# Location of the App 
$AppPath = "./myapp.sppkg"

# Add and Publish the Web Part
Write-Host 'Adding and Publishing the App..'
$App = Add-PnPApp -Path $AppPath -Scope Site -Overwrite -Publish

# Disconnect the PnP Connection
Disconnect-PnPOnline

Please note that in above code examples, -Overwrite parameter will replace the package if it is already existing and -Publish parameter will deploy/trust the package.

Important Points:
By default, app catalog is not available at site collection level, and you will need to enable it before adding a package. In case you need any help in setting up the site collection scoped app catalog, you can refer to the blog post available at: Site Collection App Catalog – Configure using PnP PowerShell (hemantkabra.in)

STEP 2: INSTALL THE SHAREPOINT APP

Once the package has been added to the app catalog and ready for installation, use below mentioned code to install the app:

# Url of the SharePoint Online Site Collection 
$SiteUrl = "https://hemantkabra.sharepoint.com/sites/mysite" 
$AppName = "HK Installation App"

# Connect to the SharePoint Online Site Collection
Write-Host "Connecting to Site Collection...!"
Connect-PnPOnline -Url $SiteUrl -Interactive

<#
Get the App from App Catalog
Change the Scope Parameter to "Tenant" if package was added at tenant level.
#>
$App = Get-PnPApp -Scope Site | Where {$_.Title -eq $AppName}

# Check if app is already installed or not
if ($null -eq $App.InstalledVersion) {
	<#
		Install the app and change the Scope Parameter to "Tenant" if package was added at tenant level!
	#>
	Write-Host 'Installing the App..'
	Install-PnPApp -Identity $App.Id -Scope Site -Wait
} elseif ($App.CanUpgrade -eq $true) {
	<#
		Update the app and change the Scope Parameter to "Tenant" if package has been added at tenant level!
	#>
	Write-Host 'Upgrading the App..'
    Update-PnPApp -Identity $App.Id -Scope Site
}

# Disconnect the Connection from the Site Collection
Disconnect-PnPOnline

Office Documentation

Please find below the official documents from the PnP community for more details about the cmdlets used in this article:

I hope you enjoyed reading this blog post. Please feel free to leave a comment in case you have any feedback, suggestions, or queries.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *