we can use ALM APIs with CSOM for this requirement.
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
Please check the below link for more details and samples
Automating add-in/app installation using ALM APIs with CSOM and PowerShell in SharePoint Online written by SharePoint Stack Exchange user Gautam Sheth
Deploy the app using App Id
string webUrl = "https://tenant-name.sharepoint.com/sites/appcatalog/";
string username = "[email protected]";
string password = "password";
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
var credentials = new SharePointOnlineCredentials(username, secureString);
using (ClientContext ctx = new ClientContext(webUrl))
{
ctx.Credentials = credentials;
var appManager = new AppManager(ctx);
var apps = appManager.GetAvailable();
var chartsApp = apps.Where(a => a.Title == "Charts 365").FirstOrDefault();
var deployApp = appManager.Deploy(chartsApp.Id, true);
//or Deploy the app async
var deployAppAsync = await appManager.DeployAsync(chartsApp.Id, true);
}
Install the app using AppMetadata
string webUrl = "https://tenant-name.sharepoint.com/sites/test/";
string username = "[email protected]";
string password = "password";
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
var credentials = new SharePointOnlineCredentials(username, secureString);
using (ClientContext ctx = new ClientContext(webUrl))
{
ctx.Credentials = credentials;
var appManager = new AppManager(ctx);
var apps = appManager.GetAvailable();
var chartsApp = apps.Where(a => a.Title == "Charts 365").FirstOrDefault();
var installApp = appManager.Install(chartsApp);
//or Install the app async
var installAppAsync = await appManager.InstallAsync(chartsApp);
}