Is it possible to add an app file to app catalog from the file system using CSOM? I having app in my local machine, i want to upload this to app catalog using CSOM. There are article to add app to site, but not able to find upload app to app catalog from CSOM. Any leads?
2 Answers
We can use the ALM APIs with CSOM for this.
Ensure that you have the latest PnP Core
version or atleast a version above November 2017.
After that, you can upload it as below:
1) Using File path
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 path = @"C:\temp\HelloWorld.sppkg";
var uploadApp = appManager.Add(path, true);
}
2) Using byte array
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 path = @"C:\temp\HelloWorld.app";
byte[] file = System.IO.File.ReadAllBytes(path);
var uploadApp = appManager.Add(file, "HelloWorld.app", true);
}
Also, if you have PnP PowerShell, you can use the Add-PnPApp
method as below:
Add-PnPApp -Path ./helloWorld.sppkg
If you want to upload it to a site collection app catalog, just modify the parameter and mention the scope.
Reference - Add-PnPApp
-
I am able to add the app to app catalog. But it is not being deployed. How deploy from CSOM?– MihirCommented Jun 11, 2018 at 15:47
-
I got it.. gist.github.com/gautamdsheth/942a9f81003ef761277da3a7808bc115– MihirCommented Jun 11, 2018 at 15:55
-
Thanks Gautam, I think the link that i have shared is yours only :)– MihirCommented Jun 12, 2018 at 13:28
-
1hey Mihir, cheers ! happy to help, you go through a blog post i have written as well which has some code snippets for the ALM APIs - gautamdsheth.wordpress.com/2017/11/24/… Commented Jun 12, 2018 at 13:35
App Catalog is also a SharePoint library, where you can directly access the files. You can upload the app file as Stream in it. I have not checked that. But we can access the Catalog using the same way we access lists and libraries.
-
Thank you. But question is not about accessing file from app catalog, adding app to app catalog using CSOM.– MihirCommented Jun 12, 2018 at 13:28