Here’s another quick-tip when working with Office 365 dev / SharePoint Online CSOM. A lot of people have asked me by e-mail, comments and twitter on how to get information about their apps in their tenants programmatically.
With that, I thought it’d be fun to post another code snippet with details on how to retrieve information about your Apps in your SharePoint Online tenant.
Pre-requisites
In order to run the code in the snippet you should already have:
- Installed the SharePoint Online CSOM NuGet into your Visual Studio project
- Installed the Office 365 Patterns and Practices NuGet into your Visual Studio project
- Have an Office 365 tenant where you’re the Tenant Administrator (or have the sufficient permissions to get the Site Properties)
Office 365 dev – Code snippet
It’s pretty straight forward to pull out information about the apps, as seen in this snippet.
using (ClientContext context = new ClientContext("https://yourdomain-admin.sharepoint.com"))
{
// Create a new Microsoft.Online.SharePoint.TenantAdministration.Tenant object and reference the client context of your Admin site
Tenant tenant = new Tenant(context);
tenant.Context.Load(tenant);
tenant.Context.ExecuteQueryRetry();
// If you define an empty string for GetAppInfoByName, you'll get ALL Apps.
var appCatalogAppInfoCollection = tenant.GetAppInfoByName(string.Empty);
tenant.Context.Load(appCatalogAppInfoCollection);
tenant.Context.ExecuteQueryRetry();
foreach (var app in appCatalogAppInfoCollection)
{
Debug.WriteLine("APP: " + app.Name);
Debug.WriteLine("Source: " + app.Source);
}
}
Snippet note: You can see I’m using ClientContext.ExecuteQueryRetry() rather then just ClientContext.ExecuteQuery(). This is an extension-method from the Office 365 PnP Core library, which I would urge you to use. In case of your request being throttled by SharePoint Online, this method will retry the action a few times (or as many times as you specify in the method as parameters).
Here’s all the AppInfo properties you can retrieve:
public string Name { get; set; }
public Guid ProductId { get; set; }
public AppSource Source { get; set; }
Enjoy!
Comments are closed
Archived comments
thank you Tobias Zimmergren.... good tips and also i havent not yet used the p&p frameworks but it looks interisting to do it in the project
Hi Sanjay,
The PnP framework is pretty neat. It helps you with common tasks and abstracts away a bunch of the glue you'd normally have to do yourself otherwise. It's a good idea to keep in mind that it's a community effort and not a finished project (e.g. do your own testin before going live with anything). Just keep up to date with the project and grab new NuGets when they come and have a go with it :-)
Tob.
Usefule tip Tobias. I have tried upload an .app file into app catalog of Apps site collection using CSOM for SP Online. But i am not able to find any alternative. Is there any?
How can I get AppCatalog library URL with CSOM code?
Is there any REST API for this? Any SOAP API?
Hi, Do you know if there is a way to change the tenant app catalog? We want to create a new site collection and make it the tenant app catalog, so we don't have to move all our apps.