Apps for Office: Mail Apps Part 2 – Read Mode Sample

Mail-Apps-For-Office-Read-Mode-Project-Folder

We continue our journey through Apps for Office with a quick tour through some of the API features in Outlook by reviewing another simple App demo. Like the Excel app, this one is not designed to solve any particular problem, but rather demonstrate some of the functionality exposed to Mail Apps. The full source code will also available be for review in the next post covering Compose mode.

Since we already looked at how to get started creating a Mail App project, let’s dive straight into the sample project. We’ll be expanding on the project we created previously, which is set to activate in both Read and Compose mode. Today we’ll look at the Read Mode and finish off with Compose in our next post.

Once again we’ll also be leveraging the Kendo UI Core widgets to help layout and navigate the different areas of the app, which is already referenced via the CDN in the sample project.

Read Mode

As we discussed in Part 1 of the Mail Apps for Office post, there are several Known Types of content, which can be discovered and acted upon by a Mail App in Read Mode. By leveraging these common types, you can create apps that react to different types of content.

We’ll add such features to our app by editing the Home.html and Home.js files in the AppRead section of the sample app that we created.

Mail-Apps-For-Office-Read-Mode-Project-Folder

The first thing we need to do is create a UI for the app, which we’ll arrange into a TabStrip, with one tab for each type of content we are detecting from the selected message or appointment.

We’ll use the API to find matching content in the message (which we’ll see shortly), then bind it via a ViewModel to the tabs using Kendo Templates to display the content in useful ways. Here is the full code for the UI:

For this app I’ve also created a test message that contains several instances of Known Entity text content that should enable the different tabs in the app.

Mail-Apps-For-Office-Read-Mode-Test-Message

We first need to define the ViewModel and functions to retrieve the content from the selected Outlook item to fill the ViewModel properties.

The sample app generated by Visual Studio when we created the project already shows how you can retrieve simple message properties:

So we’ll supplement this with our own functions to bind the list of different content items in our test message. Here’s the updated code; we’ll look at the individual bind functions next.

GetEntities

Rather than retrieve individual types of content, we can leverage a helpful method on the mailbox object, Office.context.mailbox.item.getEntities(), which returns an object with property arrays for all known entity types.

If such items are found, the array will contain the matching entries, which we can then push into the corresponding ViewModel property to be bound to the UI. Most of these content types are fairly straight forward, so we can simply define them here.

For the most part we can simply pass the entities to the ViewModel. Some entities, such as Contacts, are more complex, and to ensure that we only have the data we need, we map them to a simple JSON object as shown in the code sample above.

Launching the app reveals each of these tabs along with the detected content.

Mail-Apps-For-Office-Read-Mode-Known-Entity-Tabs

You can leverage this content in any way that makes sense for your app. Using Addresses as an example, the app will link to a Bing map of the location, which opens in a separate browser window.

Mail-Apps-For-Office-Read-Mode-Addresses

However, for URLs, I want to highlight once again that Mail Apps are not limited to simply interacting with the found content within the application. Just like all Apps for Office, Mail Apps are simply websites, running HTML and JavaScript, and can even run server-side code, allowing you to reach beyond the app to perform intensive calculations, or retrieve and process external content.

To demonstrate this, this app contains a simple Http Handler (ASHX) which accepts a URL to download and extract the page title, returning it to the app so that it can be displayed in addition to the detected URL. The handler definition simply uses a WebClient and Regex to retrieve and return the page title (see the source code for the full handler implementation).

Using AJAX, we can now call each URL that is detected within our app with the following code:

allowing us to display more relevant information to the user.

Mail-Apps-For-Office-Read-Mode-Url-Page-Title

Creating Meetings

Although we could simply display meetings in a tab just like all the other detected content item types, the Office API offers a bit more functionality we can consume to enhance the sample. By calling the method Office.context.mailbox.displayNewAppointmentForm() we can initiate the familiar Appointment dialog allowing the user to save the detected meeting to their schedule.

This method accepts a specific set of parameters which are used to pre-populate the appointment to simplify creation, again based on the detected content. Fortunately we can easily map these (if present in the detected item) and pass them to the click event of the button, as shown in this code sample.

Now when an appointment is detected, the app can respond accordingly to allow creation of an appointment.

Mail-Apps-For-Office-Read-Mode-Create-Meeting

Although such functionality is exposed already by Outlook (evidenced by the Suggested Meetings tab shown alongside our demo app), you could still leverage this within your app to suggest specific meetings based on the context besides only via the recognized sequence of text detected by Outlook.

Creating Tasks (Exchange Web Services)

Although the appointment form is handy, it is unfortunately (as of the time of this post) the only available method of its type for creating an Outlook item from the Office API.

However, this is not to say that it is not possible to create or interact with items in a user’s mailbox. In order to do so however, we must make use of the Exchange Web Services (EWS) via the generic call to Office.context.mailbox.makeEwsRequestAsync.

This method accepts two parameters. The first is an XML string which represents the SOAP request to be passed to the EWS. The second is a callback that fires on completion of the request, and contains both the result status and the resulting XML response.

Working with EWS via SOAP is mostly outside the scope of this post, however, for the purposes of the demo the sample project does implement the code to create a task in the user’s mailbox. First it creates the XML request via a helper method, populating it with the detected properties of the suggested task, then uses that as a parameter for the call to the EWS endpoint.

For our sample app, we are leveraging the CreateItem operation for tasks. Here is the sample code for creating a task, which is then wired into the click event of the create button on the UI.

For more details on working with EWS, including sample SOAP requests and responses to use in your app, be sure to review the documentation: Call web services from a mail app for Outlook

Permissions

It is important to note that in order to create items in a user’s mailbox, you must declare and request this permission in the manifest. Double click the manifest in the solution explorer and in the General tab of the manifest editor, be sure that you have selected the Permissions option for “Read write mailbox”.

Mail-Apps-For-Office-Mailbox-Permissions

Now, whenever the app detects a task, it can be created programmatically via the EWS, and saved to the user’s task list.

Mail-Apps-For-Office-Read-Mode-Create-Task
Mail-Apps-For-Office-Read-Mode-Task-Created

Wrapping Up and Next Steps

The Mail Apps offers a rich API for both detecting and interacting with various content when reading an Outlook item. Most of the API methods we used for our test email message will also function with Appointments, offering consistent behavior across the different Outlook types.

Now that we’ve seen some of the API features of a Read Mode app, we’ll wrap up our review of Mail Apps by looking at the available functions of Mail Apps in Compose Mode in our next post.

The following two tabs change content below.

Josh Morales

Developer Consultant at Falafel Software
Josh is a Microsoft Certified Professional and has been developing with Sitefinity since 2008, eventually earning Telerik MVP status and later an official Developer Support Specialist for Sitefinity at Telerik. His interest lie in expanding and extending the Sitefinity platform, adding new features and functionality to help end users make the most out of their website. Josh is also passionate about all flavors of .NET and develops apps for both Windows Phone and the Windows 8 store. Music is Josh’s other passion, and in his spare time, he spins and produces electronic music under the name DJ SelArom.