Office 365 APIs Summer Update

Scot Hillier

by Scot Hillier on 8/22/2014

Share this:

Article Details

Date Revised:

Almost as soon as I finished my article on Programming OAuth in Microsoft Azure and SharePoint 2013 Applications, Microsoft released the Office 365 API tool for Visual Studio 2013 – summer update. Because the tooling is in “preview” status, it’s reasonable to expect frequent and breaking changes. This release is no exception; it breaks the authentication code presented in my previous article. So, in this article, I’ll focus on using the new release for authentication. Complete details of the release can be found at the Office Blogs.

Obtaining the Updated Tools

You can get the updated tooling directly from Visual Studio. Simply select Tools/Extensions and Updates then search for Office 365.

Once the tools are installed, then you can create a new application. To keep it simple, create an ASP.NET MVC5 web application with No Authentication. Once the project is created, then you can right click the project in the Solution Explorer and select Add/Connected Service. This will cause the Services Manager dialog to appear.

In the Services Manager dialog, you are invited to Register You App. Clicking the link takes care of creating a new Azure Active Directory application for your project. Once this is done, you can then use the dialog to grant permissions to Office 365 resources.

After granting permissions and clicking OK, several things happen. First, the appropriate NuGet packages are downloaded to your project. Second, references are set in the project. Finally, sample code is generated. This is similar to the process used in earlier versions of the tooling, but the authentication code is quite different. In earlier versions, authentication was performed using the Authenticator class. In this release, OAuth tokens are obtained using the DiscoveryContext class. The DiscoveryContext helps you locate and connect to a desired O365 resource.

Just like earlier versions of the tooling, you can make use of classes like the ExchangeClient that provide abstractions for performing data operations against sources like calendar events, contacts, mail, and files. The sample code generated by the tooling is adequate for getting you started. If, however, you want more control of the operations, then you are likely to want to just retrieve the access token and perform the operations yourself. This is common, for example, when utilizing the SharePoint REST interface. As an example, the following code shows how to read the titles of all the lists contained in a SharePoint site from an MVC5 controller. You can follow this pattern to use the REST interface and simply change the URI as necessary.



public async Task<ActionResult> Index()


     string SharePointServiceRoot = "https://[tenant]";


     //Create discovery service

     DiscoveryContext disco = await DiscoveryContext.CreateAsync();

     ResourceDiscoveryResult rdr = null;




         //Attempt to discover resource

         rdr = await disco.DiscoverResourceAsync(SharePointServiceRoot);


     catch (RedirectRequiredException x)


         //Redirect to login page if discovery fails

         return Redirect(x.RedirectUri.ToString());



     string clientId = disco.AppIdentity.ClientId;

     string clientSecret = disco.AppIdentity.ClientSecret;

     string refreshToken = new SessionCache().Read("RefreshToken");


     //Get access token using refresh token

     ClientCredential creds = new ClientCredential(clientId, clientSecret);

     AuthenticationResult authResult = 

         await disco.AuthenticationContext.AcquireTokenByRefreshTokenAsync(

         refreshToken, creds, SharePointServiceRoot);


     string accessToken = authResult.AccessToken;


     //Execute REST call against SharePoint online

     StringBuilder requestUri = new StringBuilder(SharePointServiceRoot).Append("/_api/web/lists");


     HttpClient client = new HttpClient();

     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString());

     request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

     request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

     HttpResponseMessage response = await client.SendAsync(request);

     string responseString = await response.Content.ReadAsStringAsync();


     XElement root = XElement.Parse(responseString);

     XNamespace d = "";

     List<string> listNames = new List<string>();


     foreach (XElement entryElement in root.Descendants(d + "Title"))






     ViewBag.ListNames = listNames;

     return View();





Topic: Development

Sign in with

Or register