Use Office 365 API Tools and 365api in Windows Store applications

Source: Internet
Author: User

Use Office 365 API Tools and 365api in Windows Store applications

This article describes an API Tool used to use the Office 365 API in the Windows Store App.

First, let's talk about the background of this article:

Users who have developed SharePoint should know that SharePoint has a set of Client Object models for reading and operating lists and the data in the document library. The application types supported by this model include ASP. NET, WPF, Silverlight, and WP, but Windows Store apps are not supported (which I have never understood )... In this case, we cannot directly use this model in the Store App. What should we do if our Store App wants to use SharePoint as the server?

This is what bothers me when I first came into contact with Store apps and SharePoint. At that time, I was doing an application for Microsoft China. I needed to combine Office 365 with the Store App to read and operate Office 365 data in the Store App, integrate multiple types of data and files to form an all-in-one personal work platform, and then demonstrate the role of Office 365 in your work. It is used as a Demo promoted by Microsoft Office 365. The requirement is actually quite simple, but the most common object model is not supported, which is not that simple. Fortunately, we have another set of artifacts: SharePoint rest api reference and samples ).

SharePoint provides us with a set of standard REST APIs that allow us to read and update data through network requests. Reading data is simple. You only need to splice the API address and parse json/XML data. However, operations on data are troublesome. Merging the POST content is easy to say but tedious. What's more troublesome is the SharePoint Online authentication method. (For the use of REST APIs, I will introduce them in subsequent articles. here is just to let everyone feel how they are used .) Therefore, compared with the object model, we need to do a lot more work. This is the premise background of this Article. Find a tool to free us from the tedious work. It isMicrosoft Office 365 API Tools for Visual Studio 2013.

: Https://visualstudiogallery.msdn.microsoft.com/a15b85e6-69a7-4fdf-adda-a38066bb5155. Only Visual Studio 2013 is supported. Let's take a look at the installer information:

Public async Task <List <IContact> GetContactsAsync () {// Make sure we have a reference to the Exchange client var exchangeClient = await AuthenticationHelper. ensureOutlookClientCreatedAsync (); // Query contacts var contactsResults = await exchangeClient. me. contacts. orderBy (c => c. displayName ). executeAsync (); // Return the first page of contacts. return contactsResults. currentPage. toList ();}

public static async Task<OutlookServicesClient> EnsureOutlookClientCreatedAsync(){    try    {        AuthenticationContext = new AuthenticationContext(CommonAuthority);        if (AuthenticationContext.TokenCache.ReadItems().Count() > 0)        {            // Bind the AuthenticationContext to the authority that sourced the token in the cache             // this is needed for the cache to work when asking for a token from that authority             // (the common endpoint never triggers cache hits)             string cachedAuthority = AuthenticationContext.TokenCache.ReadItems().First().Authority;            AuthenticationContext = new AuthenticationContext(cachedAuthority);        }        // Create a DiscoveryClient using the discovery endpoint Uri.          DiscoveryClient discovery = new DiscoveryClient(DiscoveryServiceEndpointUri,            async () => await AcquireTokenAsync(AuthenticationContext, DiscoveryResourceId));        // Now get the capability that you are interested in.        CapabilityDiscoveryResult result = await discovery.DiscoverCapabilityAsync("Mail");        var client = new OutlookServicesClient(            result.ServiceEndpointUri,            async () => await AcquireTokenAsync(AuthenticationContext, result.ServiceResourceId));        return client;    }    // The following is a list of all exceptions you should consider handling in your app.    // In the case of this sample, the exceptions are handled by returning null upstream.     catch (DiscoveryFailedException dfe)    {        MessageDialogHelper.DisplayException(dfe as Exception);        // Discovery failed.        AuthenticationContext.TokenCache.Clear();        return null;    }    catch (MissingConfigurationValueException mcve)    {        MessageDialogHelper.DisplayException(mcve);        // Connected services not added correctly, or permissions not set correctly.        AuthenticationContext.TokenCache.Clear();        return null;    }    catch (AuthenticationFailedException afe)    {        MessageDialogHelper.DisplayException(afe);        // Failed to authenticate the user        AuthenticationContext.TokenCache.Clear();        return null;    }    catch (ArgumentException ae)    {        MessageDialogHelper.DisplayException(ae as Exception);        // Argument exception        AuthenticationContext.TokenCache.Clear();        return null;    }}

Here we get the Outlook Service Client, and get the token, and then use this Client to get the Outlook contact information.

Here is a complete Windows Store App Demo using Office 365 API Tools: https://github.com/officedev/office-365-apis-starter-project-for-windowsfor your reference.

 

Now, we have finished introducing Office 365 API Tools, and hope to help you develop the Store App. In the next article, we will provide a detailed introduction to the SharePoint 2013 rest api mentioned above. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.