{"id":26383,"date":"2016-07-06T10:00:43","date_gmt":"2016-07-06T17:00:43","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=26383"},"modified":"2016-07-06T10:00:43","modified_gmt":"2016-07-06T17:00:43","slug":"enable-graph-functionality-xamarin-forms-apps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/enable-graph-functionality-xamarin-forms-apps\/","title":{"rendered":"Consuming the Microsoft Graph APIs in Xamarin.Forms"},"content":{"rendered":"<p>\t\t\t\tIt&#8217;s highly likely that you&#8217;ve used at least one service powered by Microsoft Azure. Services such Mail, People, Outlook, OneDrive for Business, and SharePoint all depend on Azure for availability, scalability, reliability, and robust security. Until recently, if we wanted to interact with any of these services, we had to work with several separate APIs and SDKs.<\/p>\n<p>Recently, however, Microsoft released a <a href=\"https:\/\/graph.microsoft.io\/en-us\/\">unified API<\/a> to access all of these services, creating \u201cone endpoint to rule them all\u201d and making integrating with these services easier. The brand new Graph API is all about connecting the dots between these services and fetching contextual data with a common SDK available on all platforms that support .NET.<\/p>\n<h2>Step 1: Register the App and Authenticate<\/h2>\n<p>The first thing you&#8217;ll need to do to access the Graph API is to register your app in the Azure portal and provide access permission. As we\u2019ve done <a href=\"https:\/\/blog.xamarin.com\/authenticate-xamarin-mobile-apps-using-azure-active-directory\/\">previously<\/a>, let&#8217;s grant the required permissions.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screenshot-2016-06-28-10.39.18.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-26390\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screenshot-2016-06-28-10.39.18.png\" alt=\"Graph-Api-Permissions\" width=\"1574\" height=\"680\" \/><\/a><\/p>\n<p>Using Microsoft Active Directory Authentication Library (ADAL), authenticate and grab the <code>AccessToken<\/code>.<\/p>\n<h2>Step 2: Call the Graph API<\/h2>\n<p>Once the AccessToken is retrieved, it&#8217;s easy to call the Graph API like any other secured API using HttpClient libraries. All of these APIs can be called from the endpoint https:\/\/graph.microsoft.com. As of now, there are two versions of the Graph API, one that&#8217;s ready to use in production (1.0) and another one currently in beta. You can call the particular version by appending it to the endpoint; for example, in this blog post I\u2019m using the beta version by calling https:\/\/graph.microsoft.com\/beta.<\/p>\n<h3>Calling the User API<\/h3>\n<p>Getting user details is super easy with the Graph APIs, including a great deal lot of information about the signed in user as well as other users. You can get more details about this API from <a href=\"http:\/\/graph.microsoft.io\/en-us\/docs\/api-reference\/v1.0\/resources\/user\" target=\"_blank\">here<\/a>. The code to call the API is:<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true \" title=\"Graph API to call Users\">var client = new HttpClient();\nclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(\"application\/json\"));\nclient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(\"Bearer\", App.AuthenticationResult.AccessToken);\nvar response = await client.GetStringAsync(\"https:\/\/graph.microsoft.com\/beta\/users\");\nvar result = JsonConvert.DeserializeObject&lt;UsersRequest&gt;(response);\nUsersList.ItemsSource = result.Value;<\/pre>\n<h3>Calling the Mail API<\/h3>\n<p>Just as we could easily query users, using the Graph API we can also query emails in Office 365:<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true \" title=\"Graph API to fetch Email \">var client = new HttpClient();\nclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(\"application\/json\"));\nclient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(\"Bearer\", App.AuthenticationResult.AccessToken);\nvar response = await client.GetStringAsync(\"https:\/\/graph.microsoft.com\/beta\/me\/messages\");\nvar result = JsonConvert.DeserializeObject&lt;MailResponse&gt;(response);\nMailList.ItemsSource = result.Value;<\/pre>\n<h3>Calling the Files API<\/h3>\n<p>The Files API gives access to a user&#8217;s OneDrive for Business account. Using this access, you can manipulate files and folders:<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true \" title=\"Graph API to retrieve Files\">var client = new HttpClient();\nclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(\"application\/json\"));\nclient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(\"Bearer\", App.AuthenticationResult.AccessToken);\nvar response = await client.GetStringAsync(\"https:\/\/graph.microsoft.com\/beta\/me\/drive\/root\/children\");\nvar result = JsonConvert.DeserializeObject&lt;FileResponse&gt;(response);\nFilesList.ItemsSource = result.Value;<\/pre>\n<p>Similarly, there is an API for the Calendar, Contacts, Groups and other directory objects that can all be called from one endpoint: <code>https:\/\/graph.microsoft.com<\/code>. Now you can build your own cross-platform Outlook!<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/GraphAPIBlogPost.gif\"><img decoding=\"async\" class=\"alignnone wp-image-26403\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/GraphAPIBlogPost.gif\" alt=\"Graph API Xamarin.Forms Client App\" width=\"1160\" height=\"663\" \/><\/a><\/p>\n<h2>Wrapping Up<\/h2>\n<p>The new Graph API makes building clients for various Microsoft services easy and fun. Instead of having to use numerous different APIs and SDKs to access things like mail and files, we can access all of them easily with the Graph API. You can find more information about the Graph APIs <a href=\"http:\/\/graph.microsoft.io\/\" target=\"_blank\">here<\/a>. For a look at the Graph APIs in action with Xamarin.Forms, <a href=\"https:\/\/github.com\/mayur-tendulkar\/\" target=\"_blank\">download this sample<\/a> from GitHub.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s highly likely that you&#8217;ve used at least one service powered by Microsoft Azure. Services such Mail, People, Outlook, OneDrive for Business, and SharePoint all depend on Azure for availability, scalability, reliability, and robust security. Until recently, if we wanted to interact with any of these services, we had to work with several separate APIs [&hellip;]<\/p>\n","protected":false},"author":549,"featured_media":26403,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-26383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>It&#8217;s highly likely that you&#8217;ve used at least one service powered by Microsoft Azure. Services such Mail, People, Outlook, OneDrive for Business, and SharePoint all depend on Azure for availability, scalability, reliability, and robust security. Until recently, if we wanted to interact with any of these services, we had to work with several separate APIs [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/549"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=26383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=26383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=26383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=26383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}