Migrating from the Office 365 Discovery Service to Microsoft Graph to Discover a SharePoint Root Site 

Microsoft Graph team

The Office 365 discovery service is part of a multi-step flow required to locate and call into individual Office 365 REST API endpoints. Now you can use Microsoft Graph to access Office 365 data in a single endpoint. This reduces the complexity and improves the performance of applications connected to Office 365.

With the planned deprecation of the discovery service, you can update your app to use Microsoft Graph or to use Microsoft Graph for the discovery step. While we recommend that you move your app to Microsoft Graph entirely, in this article we will show you how to update your app to use Microsoft Graph just for the discovery step, which has less impact on existing code.  

Update your code to use Microsoft Graph for discovery

To update your code to use Microsoft Graph for discovery:

1. Change the resource parameter from https://api.office.com/discovery to https://graph.microsoft.com to request an access token. Note that you don’t need to register a new app or have your users reconsent to the existing app. You can make use of the Multi Resource Refresh Token functionality to get a token for Microsoft Graph from your exiting app that uses the Office 365 discovery service or any other direct endpoint in Office 365.

The following example shows how to get an access token by using Microsoft Graph for discovery.

string GetAccessTokenForMicrosoftGraph(string authorizationCode) 

{ 

    HttpWebRequest request = HttpWebRequest.CreateHttp("https://login.microsoftonline.com/oauth2/token"); 

    request.Method = "POST"; 

    request.ContentType = "application/x-www-form-urlencoded"; 

 

    // Replace below with the correct parameter according to your app configuration 

    byte[] requestBytes = Encoding.UTF8.GetBytes( 

        "grant_type=authorization_code" + 

        "&code=" + authorizationCode + 

        "&client_id=" + "12345678-1234-1234-1234-123456789012" + 

        "&client_secret=" + WebUtility.UrlEncode("[YourClientSecret]") + 

        "&redirect_uri=" + WebUtility.UrlEncode("https://localhost/") + 

        "&resource=" + "https://graph.microsoft.com/"); 

    // for Office 365 Discovery, resource was "https://api.office.com/discovery/" 

 

    request.GetRequestStream().Write(requestBytes, 0, requestBytes.Length); 

 

    // Get response 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

    StreamReader responseReader = new StreamReader(response.GetResponseStream()); 

 

    if (response.StatusCode == HttpStatusCode.OK) 

    { 

        string responseBody = responseReader.ReadToEnd(); 

        dynamic jwt = JsonConvert.DeserializeObject(responseBody); 

        return jwt.access_token; 

    } 

    return null; 

} 

If you can’t obtain a token for Microsoft Graph for a given user, you might need to have the user reconsent to your app with Microsoft Graph as the resource.

2. Next, update your code to use Microsoft Graph instead of the Office 365 discovery service. If your code uses the Office 365 discovery service to find a user’s SharePoint root site, you can use Microsoft Graph to find this information instead. To do this, call the SharePoint Get Site API.

The following examples show how to use the Office 365 discovery service and Microsoft Graph to get the same data.

Example using the Office 365 Discovery Service  GET https://api.office.com/discovery/v2.0/me/services  

Response: 

{ 

    "@odata.context":  "https://api.office.com/discovery/v2.0/me/$metadata#allServices", 

    "value":  [ 

                  { 

                      "@odata.type":  "#Microsoft.DiscoveryServices.ServiceInfo", 

                      "@odata.id":  "https://api.office.com/discovery/v2.0/me/services(u0027RootSite@O365_SHAREPOINT@v1.0u0027)", 

                      "@odata.editLink":  "services(u0027RootSite@O365_SHAREPOINT@v1.0u0027)", 

                      "capability":  "RootSite", 

                      "entityKey":  "RootSite@O365_SHAREPOINT@v1.0", 

                      "providerId":  "a8ef6e85-4aa3-4388-9acc-5ab592d1fcbb", 

                      "providerName":  "Microsoft", 

                      "serviceAccountType":  2, 

                      "serviceApiVersion":  "v1.0", 

                      "serviceEndpointUri":  "https://contoso.sharepoint.com/_api", 

                      "serviceId":  "O365_SHAREPOINT", 

                      "serviceName":  "Office 365 SharePoint", 

                      "serviceResourceId":  "https://contoso.sharepoint.com/" 

                  } 

              ] 

} 

Example to get the same information using Microsoft Graph

GET https://graph.microsoft.com/v1.0/sites/root/sharepointIds/siteUrl 

Response: 

{ 

"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/sharepointIds/siteUrl", 

    "value": " https://contoso.sharepoint.com " 

} 

Now that you’ve gotten the endpoint, you don’t need to make any other updates to your code. You can also update your code to call Microsoft Graph.

Conclusion 

To prepare for the upcoming deprecation of the Office 365 Discovery Service, you will need to update your code to use Microsoft Graph. The update is simple — you can choose to use Microsoft Graph to get the endpoint information as shown in this article, or you can refactor your app to use Microsoft Graph. Microsoft Graph provides the ability to access many additional resources. To learn more, see the Microsoft Graph Dev Center.

 

 

Feedback usabilla icon