Getting started with Office 365 development - Part 3 - Building a sample Web Forms application that integrates with Office 365
This article is part of a series about “Getting started with Office 365 development”.
Over a discussion a few days back someone asked if there weren’t any API’s for working with Web Forms instead of only MVC. The preview tools were previously only targeting MVC, Win Phone 8 and Windows 8 apps but have now been updated to include support for more project types, including Web Forms. See my previous article for a quick intro to using the API’s with MVC.
In this article we’ll build a sample application which will display stats about the e-mail inbox with gauges (Total Count + Unread Count).
Create a new Web Forms project
Using the latest Office 365 developer extensions for Visual Studio 2013 we are going to create a new Web Forms project that connects to and communicates with different parts of Office 365.
I’ve created a project and named it “Zimmergren.O365.VisualDashboard“.
Peak of the final result of our quest
Upon completion of this rather quick demonstration, we’ll have a final result that looks something like this:
[. More on this in the following sections.
.NET strongly typed Office 365 API’s with ASP.NET Web Forms
When using Web Forms as we are in this project, the REST API’s service description has been translated into strongly typed objects that we can get full access to through the object model which also means we’ll get a lot of help from Visual Studio with Intellisense etc.
[
- A label for Unread Email Count (with css class **stats-unreademailcount**)
- We call the **gauge.min.js** script (included in my project, downloaded [from here](https://bernii.github.io/gauge.js/))
- We call the **CustomScripts.js** which is a custom script file I’ve created, see more below.
After we’ve created this web form with the appropriate yet simple HTML markup, we’ll take a look at how we can put the data from Office 365 into play into our application.
#### Creating a custom class for MailStats
In order to hold the statistics I want to create a very simple class to hold this data. It’s called **MailStats** and looks like this:<script src="https://gist.github.com/d06152d11bde7c62f66e.js"></script>
<noscript>```
File: MailStats.cs
------------------
/// <summary>
/// Mail Statistics wrapper
/// </summary>
public class MailStats
{
/// <summary>
/// Gets or sets the Total Email count
/// </summary>
public int TotalEmails { get; set; }
/// <summary>
/// Gets or sets the Unread Email count
/// </summary>
public int UnreadEmails { get; set; }
}
#### Creating the logic for fetching the stats from Office 365
In addition to our newly created class, we need to write the logic to fetch the proper information from Office 365. The sample code from the tools provide a great starting point, and with some modifications we can easily construct it the way we want to.
In my case, I’ve created a new class called “MailApi“which essentially is a copy of the MailApiSample class which I will use as a base for my modifications.
My MailApi.cs class looks like this:
```
File: MailApi.cs
public static class MailApi
{ const string ExchangeResourceId = "https://outlook.office365.com"; const string ExchangeServiceRoot = "https://outlook.office365.com/ews/odata";
/// <summary>
/// Gets statistics about your inbox from Office 365
/// </summary>
/// <returns>MailStats object with statistics about your inbox</returns>
public static async Task<MailStats> GetMailStats()
{
MailStats stats = new MailStats();
var client = await EnsureClientCreated();
var inbox = await client.Me.Inbox.ExecuteAsync();
stats.TotalEmails = (int)inbox.TotalCount;
stats.UnreadEmails = (int)inbox.UnreadItemCount;
return stats;
}
private static async Task<ExchangeClient> EnsureClientCreated()
{
Authenticator authenticator = new Authenticator();
var authInfo = await authenticator.AuthenticateAsync(ExchangeResourceId);
return new ExchangeClient(new Uri(ExchangeServiceRoot), authInfo.GetAccessToken);
}
public static void SignOut(Uri postLogoutRedirect)
{
new Authenticator().Logout(postLogoutRedirect);
}
}
</noscript>The interesting parts of this code in our case is the GetMailStats() method which is the only thing that differs from the sample file. In this method we’ll simply make sure to call the service with a query telling Office 365 what information we want to fetch. We simply want to fetch the inbox-object with the related data (TotalCount, UnreadItemCount). #### Creating the logic for binding the data from Office 365 to your web form
So, we have a web form and we have a class and method for calling Office 365 and fetching the information we need. But how do we try this out? We start by modifying the **StatsDashboard.aspx.cs** file that we created to look like this:<script src="https://gist.github.com/1da41bbc6c39bababdad.js"></script>
<noscript>```
File: StatsDashboard.aspx.cs
----------------------------
public partial class StatsDashboard : System.Web.UI.Page
{
/// <summary>
/// We need to hook up the Page Load method to our GetMailStats asynchronous method call, which we do here.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
// Zimmergren: Hook up the GetMailStats to Page Load.
RegisterAsyncTask(new PageAsyncTask(GetMailStats));
}
/// <summary>
/// Gets the statistics in your Office 365 inbox.
/// The stats include a count of Read and Unread e-mails.
/// </summary>
/// <returns></returns>
private async Task GetMailStats()
{
// Zimmergren: Load the e-mail statistics
var stats = await MailApi.GetMailStats();
// Push the results (numbers) into the labels on the aspx page.
lblTotalEmailCount.Text = stats.TotalEmails.ToString();
lblUnreadEmailCount.Text = stats.UnreadEmails.ToString();
}
}
As you can see we bind the PageLoad method to a method with the RegisterAsyncTask method. The method we tie together with the PageLoad event is the GetMailStats() method defined in the code block. Easy as 1-2-3.
If we run the application now, we’ll see a result similar to this:
[
{ var opts = { lines: 12, angle: 0.15, lineWidth: 0.44, pointer: { length: 0.9, strokeWidth: 0.038, color: '#000000' }, limitMax: 'false',
colorStart: '#C63D0F',
colorStop: '#C63D0F',
strokeColor: '#DCE3A1',
generateGradient: true };
// Find the label for total email count and fetch the number
var mailTotalCount = document.getElementsByClassName('stats-totalemailcount')[0].innerHTML;
// Find the label for unread email count and fetch the number
var mailUnreadCount = document.getElementsByClassName('stats-unreademailcount')[0].innerHTML;
var mailTarget = document.getElementById('mail-stats-gauge'); // your canvas element
var mailGauge = new Gauge(mailTarget).setOptions(opts); // create sexy gauge!
mailGauge.maxValue = mailTotalCount; // set max gauge value
mailGauge.animationSpeed = 32; // set animation speed (32 is default value)
mailGauge.set(mailUnreadCount); // set actual value
}
$(document).ready(function () { GaugeIt(); }); ```
The application now shows some information from our Office 365 inbox and visualizes it using a small js gauge script, giving us this final result:
[, I can see that there’s 1 unread e-mail and a total of 7 e-mails just like the application states:
[![outlook](GHOST_URL/content/im
Summary
Alright, so that was an introduction to how you can get started with traditional web forms and the Office 365 API’s as well. With the sample code provided from the tools, we really get an amazing start-point for all of our coding endeavors related to Office 365.
In future posts I’m considering taking a look at how you can dig deeper into how authentication works, how you publish your applications and so on.
In any case, I hope you enjoyed this.
Cheers,
Tobias.