{"id":17407,"date":"2015-03-25T10:30:43","date_gmt":"2015-03-25T14:30:43","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=17407"},"modified":"2015-03-25T10:30:43","modified_gmt":"2015-03-25T14:30:43","slug":"add-storage-to-your-apps-with-office-365","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/add-storage-to-your-apps-with-office-365\/","title":{"rendered":"Add Storage to Your Apps with Office 365"},"content":{"rendered":"<p>\t\t\t\tEnterprises with a <i>Bring Your Own Device<\/i> (BYOD) strategy face the challenge of securing their resources on various types of devices. For example, organizations want to share documents with their employees, allow them to collaborate on documents\u00a0with coworkers, and more. Employees want to sync these documents with a variety of\u00a0devices, such as tablets, laptops and mobile phones, with a seamless search experience.\u00a0These challenges can be tackled by using OneDrive for Business, which is part of Office 365.<\/p>\n<p>Xamarin developers can use OneDrive for Business in their iOS and Android apps to download, open, edit, and upload files in Office 365 (O365). In this post, we&#8217;ll build a simple Xamarin.Android app that can connect to OneDrive for Business to show a list of files.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Note 1<\/strong><\/span>: To build apps that can connect to Office 365, you&#8217;ll need an active subscription. Also, to follow the steps manually as mentioned in this blog, you&#8217;ll need to access the O365 associated Active Directory.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Note 2<\/strong><\/span>: The\u00a0APIs, libraries, sample code and registration\u00a0process\u00a0are currently in preview.\u00a0This blog post is intended to show the possibilities of using O365 with Xamarin.<\/p>\n<h2>Step 1: Register the app<\/h2>\n<p>As we&#8217;ve seen before, Office 365 uses Azure Active Directory (AD) as the Identity Provider. Which\u00a0means that Azure Active Directory\u00a0is used to authenticate and authorize users. To allow OneDrive to be accessed from mobile apps, it is mandatory to register this app in AD.\u00a0You can follow our <a href=\"http:\/\/developer.xamarin.com\/guides\/cross-platform\/azure\/active-directory\/getting-started\/register\/\" target=\"_blank\">guide to registering an app with Active Directory<\/a> to learn how.<\/p>\n<h2>Step 2: Give access to the O365 APIs<\/h2>\n<p>On the <b>Configure<\/b> tab, click the <b>Add Application<\/b> button at the bottom.<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-01.png\"><img decoding=\"async\" class=\"alignnone size-large wp-image-17413\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-01-1024x845.png\" alt=\"Configure-Access-01\" width=\"1024\" height=\"845\" \/><\/a><\/p>\n<p>On next screen, select <b>Office 365 SharePoint Online<\/b> to access the Files and Lists.<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-02.png\"><img decoding=\"async\" class=\"alignnone size-large wp-image-17415\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-02-1024x644.png\" alt=\"Configure-Access-02\" width=\"1024\" height=\"644\" \/><\/a><\/p>\n<p>Once these services are selected, give access to the tasks that a user can perform with the app on the next screen.<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-03.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-17417\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Configure-Access-03.png\" alt=\"Configure-Access-03\" width=\"794\" height=\"317\" \/><\/a><\/p>\n<p>Now we can\u00a0<b>Save<\/b> this configuration and focus on the mobile part of our app.<\/p>\n<h2>Step 3: Build mobile app<\/h2>\n<h3>Step 3a: Refer NuGet packages<\/h3>\n<p>In order to\u00a0use O365 services in your Xamarin app, you will need to add\u00a0the following NuGet packages to your project.<\/p>\n<ul>\n<li>Azure Active Directory Authentication Library (ADAL) &#8211; v3.0.11xx (Preview\/Alpha)<\/li>\n<li>Microsoft Office 365 My Files Library for .NET &#8211; v1.0.22 (Stable)<\/li>\n<li>Microsoft Office 365 Discovery Library for .NET &#8211; v1.0.22 (Stable)<\/li>\n<\/ul>\n<h3>Step 3b: Declare variables<\/h3>\n<p>Add a new class to your project and name it <code>AuthenticationHelper<\/code>. Declare the variable at class and global level as outlined below:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic static readonly string DiscoveryServiceResourceId = &quot;https:\/\/api.office.com\/discovery\/&quot;;\npublic const string Authority = &quot;https:\/\/login.windows.net\/common&quot;;\npublic static readonly Uri DiscoveryServiceEndpointUri =\nnew Uri(&quot;https:\/\/api.office.com\/discovery\/v1.0\/me\/&quot;);\npublic static Uri returnUri = new Uri(&quot;http:\/\/Xam-O365-Integration&quot;);\npublic static string clientId = &quot;32088804-9284-451f-9ee6-2b70507a99cf&quot;;\npublic static AuthenticationContext authContext = null;\n<\/pre>\n<h3>Step 3c: Write method to acquire AccessToken<\/h3>\n<p>Using ADAL, this app will fetch the Access Token from AD, as shown below. In this method, the second\u00a0parameter will depend on the platform on which the code is executing. For example, in\u00a0Android an <code>Activity<\/code> is passed, but on iOS and Windows Phone the second\u00a0parameter is not required.<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic static async Task&lt;AuthenticationResult&gt; GetAccessToken (string serviceResourceId, Activity activity)\n{\n   authContext = new AuthenticationContext(Authority);\n   if (authContext.TokenCache.ReadItems().Count() &gt; 0)\n      authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);\n      var authResult = await authContext.AcquireTokenAsync(serviceResourceId, clientId, returnUri, new AuthorizationParameters(activity));\n   return authResult;\n}\n<\/pre>\n<h3>Step 3d: Create DiscoveryClient<\/h3>\n<p>Discovery Service helps find Service Endpoints. Create a new method <code>GetDocumentsList()<\/code>, which will create <code>DiscoveryClient<\/code> for further use.<\/p>\n<pre class=\"lang:csharp decode:true\">\nList&lt;string&gt; fileNames = new List&lt;string&gt;();\n\nDiscoveryClient discoveryClient = new DiscoveryClient(async () =&gt; {\n  var authResult = await AuthenticationHelper.GetAccessToken(AuthenticationHelper.DiscoveryServiceResourceId, this);\n  return authResult.AccessToken;\u00a0\n  });\n\nvar appCapabilities = await discoveryClient.DiscoverCapabilitiesAsync();\nvar myFilesCapability = appCapabilities.Where(s =&gt; s.Key == &quot;MyFiles&quot;)\n    .Select(p =&gt;; new { \n      Key = p.Key,\n      ServiceResourceId = p.Value.ServiceResourceId,\n      ServiceEndPointUri = p.Value.ServiceEndpointUri \n     })\n    .FirstOrDefault();\n<\/pre>\n<h3>Step 3e: Create SharePointClient<\/h3>\n<p>Files in OneDrive or Lists from SharePoint in O365 can be accessed using <code>SharePointClient<\/code>. This object again encapsulates REST services provided by O365.<\/p>\n<pre class=\"lang:csharp decode:true\">\nif (myFilesCapability != null)\n{\n  SharePointClient myFilesClient = new SharePointClient(myFilesCapability.ServiceEndPointUri,\n  async () =&gt; {\n    var authResult = await AuthenticationHelper.GetAccessToken(myFilesCapability.ServiceResourceId, this);\n    return authResult.AccessToken;\n  });\n\n  var myFilesResult = await myFilesClient.Files.ExecuteAsync();\n  do\n  {\n    var myFiles = myFilesResult.CurrentPage;\n    foreach (var myFile in myFiles)\n    {\n      fileNames.Add(myFile.Name);\n    }\n    myFilesResult = await myFilesResult.GetNextPageAsync();\n  } while (myFilesResult != null);\n}\n<\/pre>\n<h3>Step 3f: List files<\/h3>\n<p>By default, <code>MainActivity<\/code> is derived from <code>Activity<\/code>. Change this behavior to derive from <code>ListActivity<\/code> to display a list in the app.<\/p>\n<p>Just after the above code, create an adapter and use it to show the list of documents.<\/p>\n<pre class=\"lang:csharp decode:true\">\nthis.ListAdapter = new ArrayAdapter(this,\n       Android.Resource.Layout.SimpleListItem1,\n       fileNames.ToArray());\n<\/pre>\n<p>Last, call this <code>GetDocumentsList<\/code> method from the <code>OnCreate<\/code> method and run the app. It should show the following result, listing files from your OneDrive account.<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/OneDrive-Mobile-Screenshot.png\"><img decoding=\"async\" class=\"alignnone size-large wp-image-17426\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/OneDrive-Mobile-Screenshot-1024x656.png\" alt=\"OneDrive-Mobile-Screenshot\" width=\"1024\" height=\"656\" \/><\/a><\/p>\n<p>Now you&#8217;ve got a fantastic foundation to build\u00a0multi-tenant, intranet, integrated mobile apps using Office 365. Office 365 has a huge number\u00a0of features which we&#8217;ll be highlighting in future guides and samples apps.<\/p>\n<p>If you&#8217;re interested in exploring a sample project, you\u00a0can download a copy of\u00a0the source code from\u00a0this blog post on <a href=\"https:\/\/github.com\/mayur-tendulkar\/\" title=\"GitHub\" target=\"_blank\">GitHub<\/a>\u00a0and <a href=\"https:\/\/forums.xamarin.com\/36463\/\">discuss this topic in forums.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enterprises with a Bring Your Own Device (BYOD) strategy face the challenge of securing their resources on various types of devices. For example, organizations want to share documents with their employees, allow them to collaborate on documents\u00a0with coworkers, and more. Employees want to sync these documents with a variety of\u00a0devices, such as tablets, laptops and [&hellip;]<\/p>\n","protected":false},"author":549,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,3],"tags":[4],"class_list":["post-17407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-enterprise","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Enterprises with a Bring Your Own Device (BYOD) strategy face the challenge of securing their resources on various types of devices. For example, organizations want to share documents with their employees, allow them to collaborate on documents\u00a0with coworkers, and more. Employees want to sync these documents with a variety of\u00a0devices, such as tablets, laptops and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/17407","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=17407"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/17407\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=17407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=17407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=17407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}