Office 365 Dev Tip - Getting all Apps from your Tenant App Catalog using the Office 365 (CSOM) API
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!