SharePoint Application Lifecycle Management API – How to use it in your code

As described in my previous post – Install & Uninstall SharePoint apps by code announced at ESPC 2017– Microsoft announced an Application Lifecycle Management (ALM) API to installuninstalldeployretractupgrade, and remove your applications from the app catalog of your tenant and/or a specific site collection app catalog (available early 2018).

Microsoft documentation

This API is useful when you want to automate your deployment management of your SPFx solutions and add-ins, which you can do by using, e.g. a console application.
A real world scenario could be that you developed an SPFx solution for a specific set of sites and you want to push that solution onto these sites. Then you can create a console application which retrieves the ID of the solution (or app as it is called by Microsoft in the app catalog), and install it specific on these sites. If there was already an older version installed on the site, the API will upgrade the solution to the newest version that is available in the app catalog.

The PnP team did already a great job by providing an extension for their PnP CSOM project. It includes a new ALM class that contains an AppManager, the AppManager provides you the posibility to execute the specific calls as install/uninstall/upgrade… for a specific site.

How to develop this console application? This blog post will provide you a step by step guide to create the application with ‘real’ REST calls to the API and with the PnP CSOM extension.

By using PnP CSOM extension for the ALM API

Once you created a new console application project in Visual Studio, add the SharePointPnPCoreOnline Nuget Package and include the following references in your program.cs file:

  • OfficeDevPnP.Core.ALM
  • Microsoft.SharePoint.Client

First thing to do is to create a secure connection to SharePoint Online. With that connection, we can retrieve a valid ClientContext, which is necessary to create an AppManager object.

This can be done by using the SharePointOnlineCredentials implemantation that requires a username (string) and a password (SecureString).

Get available apps

From that moment we have the AppManager object, so we can execute several actions. To get more information about a specific app/solution, we can use the GetAvailable() method of the AppManager. This method will execute a call to the ALM API and returns a list of all available apps for the specific site of the ClientContext.

As you can notice, we are retrieving a list of AppMetadata that contains the following information:

Install a app on a specific site

If you want to install a specific app, you can choose to provide the complete AppMetadata object or the app/solution id to the install method.

Upgrade an app on a specific site

The property CanUpgrade indicates if their is a newer version available in the app catalog than the version that is installed on the site. If there is a newer version available, the upgrade method can be used to get the app/solution to the latest version.

Add solution package to tenant app catalog

Adding a solution (app package => .sppkg) can currently only being done to the tenant app catalog and not yet to a site collection app catalog. So this request has to be targeted to the tenant app catalog site:

Deploy solution package in tenant app catalog

Before the solution is avaible for installation on sites, it has to be enabled/deployed into the tenant. This is also only possible for the tenant app catalog, so not on site collection level.

Retract and remove solution package in the tenant app catalog

If your app is no longer needed on your tenant, you can use the retract and remove action to remove it.  This is also only possible for the tenant app catalog, so not on site collection level.

By using your own REST calls to the ALM API

As with the PnP chapter; once you created a new console application project in Visual Studio, you can add the SharePointPnPCoreOnline Nuget Package and include the following reference in your program.cs file:

  • Microsoft.SharePoint.Client

An extra thing here is that you have to take care of the Authorization header, cookies, URL’s, request method, and parsing all by yourself. This is more difficult but possible like shown in the following code example:

This response will contain a list of all the available apps for the specific site and further on you can use the ID (and not the whole metadata als with the PnP extension) to install/upgrade… an app.

Installing an app can be done by the url: /_api/web/tenantappcatalog/AvailableApps/GetById(‘GUID’)/Install, and needs to be used in a POST request:

The other calls like adding, deploying, retracting, removing.. can definitly be done by simple REST calls, but are prety much the same as described above.

Recap

As you can see the new ALM API is really powerful and can help a lot of SharePoint teams to create an automatic deploy process for their apps. It can also help these teams to push specific apps to specific sites to increase the usage and installment rate of their apps/solutions.

Creating a console application with ‘custom’ HTTP call to the API is way more difficult than using the PnP extension. So if it is possible and allowed by your internal company policy, PnP is definitely the way to go.

Extra references:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.