In this blog, we will look at steps to install and deploy SharePoint apps to Modern Sites using SharePoint ALM CSOM and PnP PowerShell. Using the below steps, it is possible to programmatically deploy and install custom SharePoint Framework apps using an Azure Function or a Local PowerShell script.
Installing SharePoint Apps
SharePoint Apps can be deployed on a site using ALM (Application Lifecycle Management) APIs. After the app is installed in the App catalog, we could add it to a SharePoint site.
The steps are pretty simple. The below snippet has the code for deploying and installing apps.
- Get the App Id
- Create an App Manager Object
- Deploy the App
- After deploy the app, install the app.
- Use Try – catch to handle if the installation has already done.
|public void DeploySingleApp(Guid appId)|
|using (ClientContext context = new ClientContext(<SiteUrl>))|
|context.Credentials = new SharePointOnlineCredentials(UserName, SecurePass);|
|AppManager manager = new AppManager(context);|
|if (manager.Deploy(appId, true))|
|if (manager.GetAvailable(appId) != null)|
|Task installTask = Task.Run(async () => await manager.InstallAsync(appId));|
|catch (Exception ex)|
|log.Info("Exception Caught – Mostly because the App is already installed. Skipping Install. Message – " + ex.InnerException.Message);|
First, lets’ get a list of apps in the App catalog.
Note: There are two values that is supported by scope paramaters for Apps – Tenant and Site.
Get-PnPApp -Scope Tenant or Get-PnPApp -Scope Site
If the App is not installed, then we will add the app to the App catalog
Add-PnPApp -Path "<.sppkg file location>" -Scope Site
Then, publish the App
Publish-PnPApp -Identity <app id> -Scope Tenant -SkipFeatureDeployment
-SkipFeatureDeployment is helpful to deploy Apps across the SharePoint tenancy if the app is developed for tenant wide deployments
After the above, we will install the App
Install-PnPApp -Identity <app id> -Scope Site
After the app is installed, it is ready to be added or used at the site.
In the upcoming blog, we will see how to add SharePoint Framework extensions and web parts programmatically.