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.

SharePoint CSOM

The steps are pretty simple. The below snippet has the code for deploying and installing apps.

  1. Get the App Id
  2. Create an App Manager Object
  3. Deploy the App
  4. After deploy the app, install the app.
  5. Use Try – catch to handle if the installation has already done.


using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.ALM;
using OfficeDevPnP.Core.Pages;
using OfficeDevPnP;
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)
{
try
{
Task installTask = Task.Run(async () => await manager.InstallAsync(appId));
installTask.Wait();
}
catch (Exception ex)
{
log.Info("Exception Caught – Mostly because the App is already installed. Skipping Install. Message – " + ex.InnerException.Message);
}
}
}
}
}

PnP PowerShell

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.

Happy Coding!!

 

Leave a comment