Winter has Arrived ! The good guys at MS have come bearing gifts.
With the ALM APIs becoming available in SharePoint online, we are now able to do ALM operations and automate add-in/app operations on Provider-hosted, SharePoint-hosted as well as SharePoint Framework applications(webparts and extensions).
These ALM APIs are now the answer to several questions such as :
How to Install an app from the app catalog using CSOM C# or PowerShell ?
How to Deploy Provider Hosted Apps (Add-Ins) / SharePoint-hosted Apps(Add-ins) / SharePoint Framework Solution(SPFx webpart and extensions) programmatically Using CSOM C# or PowerShell In SharePoint Office 365 ?
Can I automate app/add-in installation for apps in the SharePoint ?
Can I automate app/add-in installation from app-catalog ?
Using these APIs, we are able to add, install, deploy, uninstall, retract and remove operations on the SharePoint solutions .
The ever awesome PnP community has now provided these hooks via the PnP Core as well as PnP PowerShell components. OOTB support is also possible via REST APIs.
In the below post we will take a look at how to add, install, deploy, upgrade, uninstall, retract and remove apps and SPFx webparts using CSOM C# as well as PowerShell.
Here onwards, app == Provider-hosted app or SharePoint-hosted app or SharePoint Framework Webpart or extension
Firstly, you need to install the latest PnP Core dll or PnP PowerShell (Nov 2017 or higher) to perform these operations as these are not supported via OOTB CSOM components currently.
Now, to get list of all available apps in app catalog, you can use the below code.:
To, upload an app, there are 2 methods. You can pass the file name or you can pass the byte array.
They are as below:
1) Upload using file path
2) Upload using byte array
Now, that we have uploaded the app, we need to deploy the app so that it can available to be installed in the sites.
Again, there are 2 methods. You can the appMetadata or you can pass the GUID of the app.
They are as below:
1) Deploy the app using AppMetadata
2) Deploy the app using App Id
After deploy the app, it will be available for installation.
There are 2 methods using which you can install the app in a site collection.
They are as below:
1) Install the app using AppMetadata
2) Install the app using App Id
Also, if you want to upgrade the app, you can use the Upgrade method.
1) Upgrade the app using AppMetadata
2) Upgrade the app using App Id
After installation/upgradation of the app, should you decide to uninstall the app from the site collection, you can use the below 2 snippets. It will do a clean uninstall. We dont need to remove it from the recycle bin as it will handled itself
1) Uninstall the app using AppMetadata
2) Uninstall the app using App Id
If you want to retract the app, you can use the Retract method. When you retract the app, it will remain unavailable for installation in the sites.
1) Retract app using AppMetadata
2) Retract app using App GUID
If you want to completely remove from the App Catalog, you can use the Remove
method. It will be a clean removal and the app will be removed from the recycle bin as well.
1) Remove app using AppMetadata
2) Remove app using App GUID
To use these above methods, you need to download the latest PnP CSOM core component. The version should be 2.20.1711 or higher.
Download and install it from the the nuget.
The equivalent methods in PnP PowerShell are listed below:
In the above code samples, I am using userName and password to authenticate. However, app-only authentication will also work i.e you can just provide a client id and secret with necessary permissions and make it work by somewhat as:
//SharePoint Online - App Only
string siteUrl = "https://tenant-name.sharepoint.com";
string acsAppId = "70DA500D-6000-48D4-AA1F-22793A5FE814";
string acsSupport = GetString("ACS App Secret");
AuthenticationManager authManager = new AuthenticationManager();
ClientContext context = authManager.GetAppOnlyAuthenticatedContext(siteUrl, acsAppId, acsSupport);
Please note – These methods will only work on SharePoint online at the moment. If and when MS pushes it out to On-Premises, will update the post to reflect the same.
PS – Currently, upload, retract and remove operations will work only on the App catalog site. However, with the Site-collection specific App catalog being rolled out in coming days, they will be supported for those site collections as well.
Authenticate to SharePoint using PnP Authentication Manager
PnP PowerShell download link(Nov 2017 or higher) – PnP November 2017 Release