in Office 365, Office 365 API

Working with Office 365 Discovery Service

Office 365 Discovery Service helps you to discover the available service capabilities for your app along with the respective service resource Ids and endpoint URIs.

Why Discovery Service?

Let us consider the following scenario:

For example: To read a user’s files from OneDrive, your app first needs to:

  • Get access token for the MyFiles resource
  • Create the SharePoint client with the right OneDrive endpoint

Given that every user is going to have their unique URI for their OneDrive portal, it is difficult to guess what that endpoint would be. Worse, if you are ISV and publishing the app, your app would need to know the users’ OneDrive endpoint for any given tenant!

Discovery Service to the rescue!

With the Office 365 Discovery Service, you can easily query the resource Id and endpoint URI for the service your app wants to connect. In our scenario, your app will query for MyFiles.

What does Discovery Service result return?

Discovery service returns the following key things your app will need to successfully get access token and create the respective service client:

  • Service Capability Resource Id
  • Service Endpoint URI

When your app queries the discovery service for the services available, it will return all the services that your app has permissions to. For example:

  • If your app has access to Mail, Calendar and OneDrive, then then discovery service will only return the result for Mail, Calendar and OneDrive.
  • If you try querying service info for Contacts, then it will fail as your app does not have permissions to Contacts permissions.

How to query Discovery Service in my app?

If you are using the Office 365 API Tools for Visual Studio 2013, then the tool adds the following Office 365 Discovery Service NuGet package to your .NET project.

If you are not using the tool, then you can add it manually via the Package Manager Console:

Install-Package Microsoft.Office365.Discovery

Once added, you can then create the DiscoveryClient with which you can query available service info.

As with any service in the cloud, your app needs to first get access token for the discovery service itself.

For full sample, fork or clone this Office 365 Discovery Service Sample Project

And once you have the client initialized and ready to go, you can query the available service capabilities:

Caching the Discovery Service Result

Since the discovery service is going to return the capabilities that your app has permissions to, that is, in most cases, not going to change unless you publish a new version that includes new capabilities too.

So, instead of querying discovery service every time for the service information, you can cache the discovery result and read from the cache every time you create the respective service client. This will improve the performance of your app too as it can save round trips to the discovery service.

The Office 365 Discovery Service Sample shows how a Windows Store app can benefit from caching the discovery service result info.

Feel free to leave a comment below if you have any questions!

If you have a product/feature request, make sure you post your idea here!

Happy Coding!


Write a Comment