{"id":12567,"date":"2014-07-30T12:06:25","date_gmt":"2014-07-30T16:06:25","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=12567"},"modified":"2014-07-30T12:06:25","modified_gmt":"2014-07-30T16:06:25","slug":"put-some-office-365-into-your-apps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/put-some-office-365-into-your-apps\/","title":{"rendered":"Put Some Office 365 in Your Apps"},"content":{"rendered":"<p>\t\t\t\tOffice 365 is Microsoft&#8217;s cloud-based solution providing access to services like Mail, Calendar, Contacts (using Exchange Online), Storage (using OneDrive) and Collaboration tools (using SharePoint Online). These services are backed by Azure&#8217;s robust infrastructure and use Microsoft Azure Active Directory (AD) to authenticate, authorize and grant access to users. <img decoding=\"async\" class=\"alignright size-thumbnail wp-image-12945\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/28a3b05a2f36bc930f86ad1ad8031219b50a0e5f-150x150.png\" alt=\"Office 365 Logo\" width=\"150\" height=\"150\" \/>However, developers like us have always want to extend these services in some way. For this reason, Microsoft has released Client Libraries for Office 365 and, good news, they are Portable Class Libraries! This means these libraries can be used in Xamarin.Android and Xamarin.iOS applications. You can read more in the <a title=\"here\" href=\"http:\/\/blogs.office.com\/2014\/05\/12\/net-and-javascript-libraries-for-office-365-apis\/\">official post from Microsoft<\/a>.<\/p>\n<p>In this post, we&#8217;ll build a Xamarin.Android app, which will connect to the Office 365 service. We&#8217;ll extend this app to show data from our Office 365 subscription. The app will talk to our Office 365 Exchange Inbox and fetch mails from there. The overall app will look similar to the screenshots shown below:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-12568 size-full\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img1.png\" alt=\"xamarin-o365-img1\" width=\"1185\" height=\"526\" \/><\/p>\n<h2>Step 1: Get the Tooling<\/h2>\n<p>To build apps that can connect with Office 365, we need to install the necessary tooling support. For Visual Studio:<\/p>\n<ul>\n<li>On the Visual Studio menu, click Tools-&gt;Extensions and Updates-&gt;Updates.<\/li>\n<li>You should see the update available for Office 365 API Tools.<\/li>\n<li>Click Update to update to the latest version.<\/li>\n<\/ul>\n<p>\u2013OR\u2013<\/p>\n<ul>\n<li><a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/7e947621-ef93-4de7-93d3-d796c43ba34f\">Download<\/a> the extension and install it manually.<\/li>\n<\/ul>\n<h2>Step 2: Create Xamarin.Android Application<\/h2>\n<p>For this sample, we&#8217;ll choose the default Android app template and extend this app to use Office 365 libraries.<\/p>\n<ul>\n<li>In Visual Studio, File &gt; New &gt; Project<\/li>\n<li>Select C# &gt; Android and Blank App (Android) project template<\/li>\n<li>Give project a name (in my case O365Client) and click \u2018OK\u2019<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"alignnone wp-image-12571 size-full\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img2.png\" alt=\"xamarin-o365-img2\" width=\"624\" height=\"431\" \/><\/p>\n<p>This will create a default &#8220;Hello World&#8221; app, which shows the incremental number of clicks of a button.<\/p>\n<h2>Step 3: Add Office 365 Connected Service<\/h2>\n<p>To use Office 365 Services from this app, we need to add libraries and reference the services. Simply right click the project in the Solution Explorer and add a connected service.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12576\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img3.png\" alt=\"xamarin-o365-img3\" width=\"544\" height=\"155\" \/><\/p>\n<p>On the next screen, sign in with your Office 365 Account:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12577\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img4.png\" alt=\"xamarin-o365-img4\" width=\"624\" height=\"430\" \/><\/p>\n<p>Now, select the service which you want to access and give it a permission and click &#8220;OK&#8221;:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12578\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img5.png\" alt=\"xamarin-o365-img5\" width=\"624\" height=\"432\" \/><\/p>\n<h2>Step 4: Using the Services<\/h2>\n<p>In the Solution Explorer, you&#8217;ll find that the tooling has added the required assemblies as well as some C# files with sample code:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12582\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xamarin-o365-img6.png\" alt=\"xamarin-o365-img6\" width=\"348\" height=\"687\" \/><\/p>\n<pre class=\"lang:csharp decode:true\">\n[Application]\npublic class MyGlobalDeclaration : Android.App.Application\n{\n  public MyGlobalDeclaration (IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)\n  {\n  }\n  public static List Messages { get; set;}\n  public override void OnCreate()\n  {\n    base.OnCreate();\n    Messages = new List ();\n  }\n}\n<\/pre>\n<p>To show data in a specific format (Inbox layout) we need to add a custom adapter, which will populate this view. Add a new class to the project and modify it as displayed below:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class HomeScreenAdapter : BaseAdapter\n{\n  List items; Activity context;\n  public HomeScreenAdapter(Activity context, IEnumerable items) : base()\n  {\n     this.context = context;\n     this.items = items.ToList();\n  }\n  public override long GetItemId(int position)\n  {\n    return position;\n  }\n  public override IMessage this[int position]\n  {\n    get { return items[position]; }\n  }\n  public override int Count\n  {\n    get { return items.Count; }\n  }\n  public override View GetView(int position, View convertView, ViewGroup parent)\n  {\n    View view = convertView; \/\/ re-use an existing view, if one is available\n    if (view == null) \/\/ otherwise create a new one\n    view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);\n    view.FindViewById(Android.Resource.Id.Text1).Text = items[position].Subject;\n    view.FindViewById(Android.Resource.Id.Text2).Text = items[position].From.Address;\n    view.Tag = items [position].Id;\n    return view;\n   }\n}\n<\/pre>\n<p>Now, let&#8217;s modify &#8216;MainActivity.CS&#8217; to show our Inbox.First, the activity needs to inherit from ListActivity to display records in a list mode:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class MainActivity : ListActivity\n<\/pre>\n<p>To authenticate against the Inbox in Office 365 i.e. Exchange, we\u2019ll declare two endpoints:<\/p>\n<pre class=\"lang:csharp decode:true\">\nconst string ExchangeResourceId = &quot;https:\/\/outlook.office365.com&quot;;\nconst string ExchangeServiceRoot = &quot;https:\/\/outlook.office365.com\/ews\/odata&quot;;\n<\/pre>\n<p>This is the perfect time to authenticate, we&#8217;ll write the <strong>EnsureClientCreated<\/strong> method, which will create an Office365 client that we can use to call the services. To execute this method we&#8217;ll pass the current activity as the context.<\/p>\n<pre class=\"lang:csharp decode:true\">\nprivate async Task EnsureClientCreated(Context context)\n{\n  Authenticator authenticator = new Authenticator(context);\n  var authInfo = await authenticator.AuthenticateAsync(ExchangeResourceId);\n  return new ExchangeClient(new Uri(ExchangeServiceRoot), authInfo.GetAccessToken);\n}\n<\/pre>\n<p>Once the client is ready we can call <strong>GetMessages<\/strong>, which gets messages from our Office 365 Inbox.<\/p>\n<pre class=\"lang:csharp decode:true\">public async Task GetMessages(Context context)\n{\n  var client = await EnsureClientCreated(context);\n  var messageResults = await (from i in client.Me.Inbox.Messages\n      orderby i.DateTimeSent descending\n      select i).ExecuteAsync();\n  MyGlobalDeclaration.Messages = messageResults.CurrentPage.ToList ();\n  ListAdapter = new HomeScreenAdapter(this, MyGlobalDeclaration.Messages);\n  return messageResults.CurrentPage;\n}\n<\/pre>\n<p>However, we&#8217;re yet to call <strong>GetMessages<\/strong> method in our code. Modify the OnCreate method as shown below to call this method and fetch messages.<\/p>\n<pre class=\"lang:csharp decode:true\">protected override void OnCreate(Bundle bundle)\n{\n  base.OnCreate(bundle);\n  GetMessages (this);\n}\n<\/pre>\n<p>When the user clicks on a particular email message, we want to show that message. First, add an <strong>OnListItemClick<\/strong> method, which, when clicked, will pass the Email&#8217;s ID to next activity, which we&#8217;ll add later.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\nprotected override void OnListItemClick (ListView l, View v, int position, long id)\n{\n  var bodyActivity = new Intent (this, typeof(MessageBodyActivity));\n  bodyActivity.PutExtra (&quot;Id&quot;, v.Tag.ToString());\n  StartActivity (bodyActivity);\n}\n\n<\/pre>\n<p>Now, we&#8217;ll add a new layout and activity for the detailed message. To do this, go to Resources &gt; Layouts and add a new Android Layout. I&#8217;ve given it a name &#8220;Message.axml&#8221;.\nModify this file as shown below to include our view.<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--&gt;\n\n<\/pre>\n<p>Here, add a new Android Activity to the project and modify it with following code to display the message and sender&#8217;s email ID.<\/p>\n<pre class=\"lang:csharp decode:true\">\n[Activity (Label = &quot;Email:&quot;)]\npublic class MessageBodyActivity : Activity\n{\n  protected override void OnCreate (Bundle bundle)\n  {\n    base.OnCreate (bundle);\n    var id = Intent.GetStringExtra (&quot;Id&quot;);\n    if(string.IsNullOrWhiteSpace(id))\n      return;\n\n    var message = MyGlobalDeclaration.Messages.FirstOrDefault (m =&gt; m.Id == id);\n\n    if(message == null)\n      return;\n\n    SetContentView (Resource.Layout.Message);\n    var fromText = FindViewById (Resource.Id.fromText);\n    var bodyText = FindViewById (Resource.Id.bodyText);\n    fromText.Text = message.From.Address;\n    bodyText.SetText(Html.FromHtml(message.Body.Content), TextView.BufferType.Spannable);\n  }\n}\n<\/pre>\n<p>This completes our amazing Office 365-enabled mobile app! Debug the application, and login with your Office 365 account. This should show all the mails in your inbox, and, if you click on any mail item, it should take you to its detailed view.<\/p>\n<p>I want to reiterate here that you are not just limited to fetching mails from the inbox like we did in this example, but you can fetch calendar events, get files from OneDrive for Business, or even browse through sites available under SharePoint Online.<\/p>\n<p>I hope that this was a great introduction on how you can use the brand new Office365 API in your Xamarin.iOS and Xamarin.Android applications. You can <a href=\"https:\/\/github.com\/mayur-tendulkar\/\">download the full source code<\/a> for this sample on my GitHub.<\/p>\n<p><a title=\"Discuss in Forums\" target=\"_blank\"><a href=\"http:\/\/forums.xamarin.com\/discussion\/21367\/put-some-office-365-in-your-apps#latest\"><em>Discuss this blog post in the Xamarin Forums<\/em><\/a><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Office 365 is Microsoft&#8217;s cloud-based solution providing access to services like Mail, Calendar, Contacts (using Exchange Online), Storage (using OneDrive) and Collaboration tools (using SharePoint Online). These services are backed by Azure&#8217;s robust infrastructure and use Microsoft Azure Active Directory (AD) to authenticate, authorize and grant access to users. However, developers like us have always [&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],"tags":[4],"class_list":["post-12567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Office 365 is Microsoft&#8217;s cloud-based solution providing access to services like Mail, Calendar, Contacts (using Exchange Online), Storage (using OneDrive) and Collaboration tools (using SharePoint Online). These services are backed by Azure&#8217;s robust infrastructure and use Microsoft Azure Active Directory (AD) to authenticate, authorize and grant access to users. However, developers like us have always [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12567","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=12567"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12567\/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=12567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=12567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=12567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}