Created
March 24, 2014 21:55
-
-
Save ahelland/9750060 to your computer and use it in GitHub Desktop.
Code for the blog post "Microsoft Provides a RESTful API for Exchange - Part 2" on http://mobilitydojo.net/2014/03/24/microsoft-provides-a-restful-api-for-exchange-part-2/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CustomOffice365OWA.Models; | |
using System; | |
using System.Globalization; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
namespace CustomOffice365OWA.Controllers | |
{ | |
[RoutePrefix("Mail")] | |
public class ExchangeController : Controller | |
{ | |
[HttpGet] | |
public async Task<ActionResult> Index() | |
{ | |
// Obtain information for communicating with the service: | |
Office365ServiceInfo serviceInfo = Office365ServiceInfo.GetExchangeServiceInfo(); | |
if (!serviceInfo.HasValidAccessToken) | |
{ | |
return Redirect(serviceInfo.GetAuthorizationUrl(Request.Url)); | |
} | |
string requestUrl = String.Format(CultureInfo.InvariantCulture, | |
"{0}/Me/Inbox/Messages", | |
serviceInfo.ApiEndpoint | |
); | |
// Prepare the HTTP request: | |
using (HttpClient client = new HttpClient()) | |
{ | |
Func<HttpRequestMessage> requestCreator = () => | |
{ | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); | |
request.Headers.Add("Accept", "application/json;odata.metadata=full"); | |
return request; | |
}; | |
// Send the request using a helper method, which will add an authorization header to the request, | |
// and automatically retry with a new token if the existing one has expired. | |
using (HttpResponseMessage response = await Office365CommonController.SendRequestAsync( | |
serviceInfo, client, requestCreator)) | |
{ | |
// Read the response and deserialize the data: | |
string responseString = await response.Content.ReadAsStringAsync(); | |
if (!response.IsSuccessStatusCode) | |
{ | |
return Office365CommonController.ShowErrorMessage(serviceInfo, responseString); | |
} | |
var messageContext = Newtonsoft.Json.JsonConvert.DeserializeObject<Message_odataContext>(responseString); | |
var messages = messageContext.Messages; | |
return View(messages); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
namespace CustomOffice365OWA.Models | |
{ | |
#region Email | |
public class Message_odataContext | |
{ | |
[JsonProperty("@odata.context")] | |
public string context; | |
[JsonProperty("value")] | |
public List<Message> Messages { get; set; } | |
} | |
public class Message | |
{ | |
[JsonProperty("@odata.id")] | |
[ScaffoldColumn(false)] | |
public string odataId { get; set; } | |
[JsonProperty("@odata.editLink")] | |
[ScaffoldColumn(false)] | |
public string odataEditLink { get; set; } | |
[ScaffoldColumn(false)] | |
public string Id { get; set; } | |
[ScaffoldColumn(false)] | |
public string ChangeKey { get; set; } | |
[ScaffoldColumn(false)] | |
public string ConversationId { get; set; } | |
[ScaffoldColumn(false)] | |
public string ParentFolderId { get; set; } | |
[JsonProperty("Attachments@odata.navigationLink")] | |
[ScaffoldColumn(false)] | |
public string AttachmentLink { get; set; } | |
[ScaffoldColumn(false)] | |
public string EventId { get; set; } | |
public DateTime DateTimeReceived { get; set; } | |
public DateTime DateTimeSent { get; set; } | |
public DateTime DateTimeCreated { get; set; } | |
public DateTime LastModifiedTime { get; set; } | |
public Recipient From { get; set; } | |
public Recipient Sender { get; set; } | |
public Recipient[] ToRecipients { get; set; } | |
public string Subject { get; set; } | |
public string BodyPreview { get; set; } | |
public Body Body { get; set; } | |
public string Importance { get; set; } | |
public Recipient[] CcRecipients { get; set; } | |
public Recipient[] BccRecipients { get; set; } | |
public Recipient[] ReplyTo { get; set; } | |
public string IsDeliveryReceiptRequested { get; set; } | |
public string IsReadReceiptRequested { get; set; } | |
public string IsDraft { get; set; } | |
public string IsRead { get; set; } | |
public string MeetingMessageType { get; set; } | |
public string HasAttachments { get; set; } | |
public Category[] Categories { get; set; } | |
public string ClassName { get; set; } | |
} | |
public class Recipient | |
{ | |
public string Name { get; set; } | |
public string Address { get; set; } | |
} | |
public class Body | |
{ | |
public string ContentType { get; set;} | |
public string Content {get;set;} | |
} | |
public class Category | |
{ | |
public string Name {get;set;} | |
} | |
#endregion | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model IEnumerable<CustomOffice365OWA.Models.Message> | |
@{ | |
ViewBag.Title = "Mails"; | |
} | |
<table class="table table-bordered table-striped"> | |
<tr> | |
<th>@Html.DisplayNameFor(model => model.DateTimeReceived)</th> | |
<th>@Html.DisplayNameFor(model => model.From.Name)</th> | |
<th>@Html.DisplayNameFor(model => model.Sender.Address)</th> | |
<th>@Html.DisplayNameFor(model => model.Importance)</th> | |
<th>@Html.DisplayNameFor(model => model.Subject)</th> | |
<th>@Html.DisplayNameFor(model => model.BodyPreview)</th> | |
<th>@Html.DisplayNameFor(model => model.Body.Content)</th> | |
<th>@Html.DisplayNameFor(model => model.ClassName)</th> | |
<th>@Html.DisplayNameFor(model => model.MeetingMessageType)</th> | |
<th>@Html.DisplayNameFor(model => model.IsDeliveryReceiptRequested)</th> | |
<th>@Html.DisplayNameFor(model => model.IsReadReceiptRequested)</th> | |
<th>@Html.DisplayNameFor(model => model.IsDraft)</th> | |
<th>@Html.DisplayNameFor(model => model.IsRead)</th> | |
<th>@Html.DisplayNameFor(model => model.HasAttachments)</th> | |
<th>@Html.DisplayNameFor(model => model.DateTimeSent)</th> | |
<th>@Html.DisplayNameFor(model => model.DateTimeCreated)</th> | |
<th>@Html.DisplayNameFor(model => model.LastModifiedTime)</th> | |
</tr> | |
@foreach (var item in Model) { | |
<tr> | |
<td>@Html.DisplayFor(modelItem => item.DateTimeReceived)</td> | |
<th>@Html.DisplayFor(modelItem => item.From.Name)</th> | |
<th>@Html.DisplayFor(modelItem => item.Sender.Address)</th> | |
<td>@Html.DisplayFor(modelItem => item.Importance)</td> | |
<td>@Html.DisplayFor(modelItem => item.Subject)</td> | |
<td>@Html.DisplayFor(modelItem => item.BodyPreview)</td> | |
<td>@Html.DisplayFor(modelItem => item.Body.Content)</td> | |
<td>@Html.DisplayFor(modelItem => item.ClassName)</td> | |
<td>@Html.DisplayFor(modelItem => item.MeetingMessageType)</td> | |
<td>@Html.DisplayFor(modelItem => item.IsDeliveryReceiptRequested)</td> | |
<td>@Html.DisplayFor(modelItem => item.IsReadReceiptRequested)</td> | |
<td>@Html.DisplayFor(modelItem => item.IsDraft)</td> | |
<td>@Html.DisplayFor(modelItem => item.IsRead)</td> | |
<td>@Html.DisplayFor(modelItem => item.HasAttachments)</td> | |
<td>@Html.DisplayFor(modelItem => item.DateTimeSent)</td> | |
<td>@Html.DisplayFor(modelItem => item.DateTimeCreated)</td> | |
<td>@Html.DisplayFor(modelItem => item.LastModifiedTime)</td> | |
</tr> | |
} | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment