Skip to content

This sample uses the Office 365 API Tools to demonstrate basic operations against the Calendar, Contacts, and Mail service endpoints in Office 365 from a single-tenant ASP.NET MVC application.

License

Notifications You must be signed in to change notification settings

rheid/Office-365-APIs-Starter-Project-for-ASPNETMVC

 
 

Repository files navigation

Office 365 Starter Project for ASP.NET MVC

Table of Contents

Overview

This sample uses the Office 365 APIs client libraries to demonstrate basic operations against the Calendar, Contacts, and Mail service endpoints in Office 365 from a single-tenant ASP.NET MVC 5 application.

Below are the operations that you can perform with this sample:

Calendar

  • Read events
  • Add events
  • Refresh the calendar
  • Update events
  • Remove events

Contacts

  • Add contacts
  • Refresh the contacts list
  • Update contacts
  • Remove contacts

Mail

  • Read email messages
  • Create and send a new email

Users and Groups

  • Sign in/out

Prerequisites and Configuration

This sample requires the following:

Configure the sample

Follow these steps to configure the sample.

  1. Open the O365-APIs-Start-ASPNET-MVC.sln file using Visual Studio 2013.
  2. Register and configure the app to consume Office 365 services (detailed below).
  3. Get your Office 365 tenant ID from Microsoft Azure (detailed below).

Register app to consume Office 365 APIs

You can do this via the Office 365 API Tools for Visual Studio (which automates the registration process). Be sure to download and install the Office 365 API tools from the Visual Studio Gallery.

  1. Build the project. This will restore the NuGet packages for this solution.
  2. In the Solution Explorer window, choose O365-APIs-Start-ASPNET-MVC project -> Add -> Connected Service.
  3. A Services Manager window will appear. Choose Office 365 and Register your app.
  4. On the sign-in dialog box, enter the user name and password for your Office 365 tenant. We recommend that you use your Office 365 Developer Site. Often, this user name will follow the pattern @.onmicrosoft.com. If you do not have a developer site, you can get a free Developer Site as part of your MSDN Benefits or sign up for a free trial. Be aware that the user must be a Tenant Admin user—but for tenants created as part of an Office 365 Developer Site, this is likely to be the case already. Also developer accounts are usually limited to one sign-in.
  5. After you're signed in, you will see a list of all the services. Initially, no permissions will be selected, as the app is not registered to consume any services yet.
  6. To register for the services used in this sample, choose the following permissions, and select the Permissions link to set the following permissions: - (Calendar) – Have full access to users’ calendar and Read users' calendars - (Contacts) – Have full access to users’ contacts and Read users' contacts - (Mail) - Send mail as a user, Read and write access to users' mail, and Read users' mail - (Users and Groups) – Enable sign-on and read users’ profiles
  7. Choose the App Properties link in the Services Manager window. Make this app available to a Single Organization.
  8. After clicking OK in the Services Manager window, assemblies for connecting to Office 365 REST APIs will be added to your project.
  9. Build the solution.

Get your Office 365 tenant ID from Microsoft Azure

In order to complete this procedure, you're going to need to log into the Microsoft Azure management portal. To do this you must have an Azure subscription. A free trial is available if you do not currently have one. You can sign up here: http://azure.microsoft.com/en-us/pricing/free-trial/. You must also ensure you have already completed the Register app to consume Office 365 APIs procedure.

Note: You will also need to ensure your Azure subscription is bound to your Office 365 tenant. To do this see the Active Directory team's blog post, Creating and Managing Multiple Windows Azure Active Directories. The section Adding a new directory will explain how to do this. You can also read Set up Azure Active Directory access for your Developer Site for more information.

To retrieve your Office 365 tenant ID:

  1. Sign into the Azure management portal at https://manage.windowsazure.com/.
  2. Select the Active Directory tab in the left pane and choose your target Office 365 domain underneath the back button. As a reminder you must have the Azure subscription configured to use your specific Office 365 tenant.
![](http://i.imgur.com/SU8Ri5f.png)
  1. Choose the Applications tab for your domain and select the registration entry for your app. It should appear as something like O365-APIs-Start-ASPNET-MVC.OfficeO365App.
![](http://i.imgur.com/5dtWcua.png)
  1. Upon clicking that entry, expand the Enable Users To Sign On section, copy and paste the Federation Metadata Document URL value to notepad or another application. You'll notice that there's an identifier present in that URL (in the form of a guid), and this is the tenant ID that is needed for the project.
![](http://i.imgur.com/TzXIlut.png)
  1. Copy just the identifier value and return to the sample solution. In Solution Explorer expand the Utils folder and open AADAppSettings.cs.
  2. Add your tenant ID to the _authority field. It should look similar to this: private static string _authority = "https://login.windows.net/g80f86bc-4df0-8wxb-cf96-673d5bca01ad";
  3. You are now ready to build the project.

Note: If you are deploying to a production tenant, you will need to ask your tenant admin for the tenant identifier.

Build

After you've loaded the solution in Visual Studio, press F5 to build and debug. Run the solution and sign in with your organizational account to Office 365.

Project Files of Interest

Controllers

  • AccountController.cs
  • CalendarController.cs
  • ContactController.cs
  • MailContoller.cs

Helper Classes

  • CalendarOperations.cs
  • ContactOperations.cs
  • MailOperations
  • AuthenticationHelper.cs

Models

  • CalendarEvent.cs
  • ContactItem.cs
  • IdentityModels.cs
  • MailItem.cs

Utils Folder

  • AADAppSettings.cs
  • NaiveSessionCache.cs - This is a sample token cache and should not be used in a production environment. We suggest that you store and interact with tokens in accordance with the security policy of your organization.

Views

  • Calendar/Create.cshtml
  • Calendar/Delete.cshtml
  • Calendar/Edit.cshtml
  • Calendar/Index.cshtml
  • Contact/Create.cshtml
  • Contact/Delete.cshtml
  • Contact/Edit.cshtml
  • Contact/Index.cshtml
  • Mail/Create.cshtml
  • Mail/Delete.cshtml
  • Mail/Index.cshtml
  • Shared/_Layout.cshtml
  • Shared/_LoginPartial.cshtml

Other

  • RouteConfig.cs
  • web.config
  • Startup.cs
  • packages.config

Troubleshooting

If you see any errors while installing packages, for example, Unable to find "Microsoft.Azure.ActiveDirectory.GraphClient" version="1.0.21", make sure the local path where you placed the solution is not too long/deep. Moving the solution closer to the root of your drive resolves this issue. We'll also work on shortening the folder names in a future update.

Copyright

Copyright (c) Microsoft. All rights reserved.

About

This sample uses the Office 365 API Tools to demonstrate basic operations against the Calendar, Contacts, and Mail service endpoints in Office 365 from a single-tenant ASP.NET MVC application.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 61.6%
  • JavaScript 31.8%
  • CSS 6.6%