Hands-On Lab

Getting Started with the EWS Managed API 1.1 – Working with Mailbox Items

Lab version: 0.9

Last updated: 4/14/2011


Contents

Overview 3

System Requirements 3

Exercise 1: Working with Calendar Items 3

Task 1 – Beginning the Lab 3

Task 2 – Establishing a connection to the Exchange Server 4

Task 3 – Creating Items 4

Task 4 – Finding Items 6

Task 5 – Impersonation 7

Summary 8

Overview

Lab Time: 45 minutes

Lab Folder: C:\%Office365TrainingKit%\Labs\10.1\Source\Before

The After folder contains the completed lab exercises.

Lab Overview: The Microsoft Exchange Web Services (EWS) Managed API 1.1 provides an intuitive managed API for developing client and server applications that leverage Exchange data and business logic, whether Exchange is running on premises or in the cloud. The EWS Managed API 1.1 makes Exchange Web Services SOAP calls “under the covers”; so many environments are already configured for EWS Managed API 1.1.

In this lab, you will use the EWS Managed API 1.1 to:

·  Prepare the API to connect to the correct Exchange CAS server for your calling user.

·  Work with mailbox items.

·  Find mailbox items using the example of retrieving appointments.

·  Impersonate users so work done with the API is done using the impersonated user’s credentials.

System Requirements

You must have the following items to complete this lab:

·  Microsoft Visual Studio 2010

·  Exchange Web Services Managed API 1.1 SDK

·  Two accounts configured to have Microsoft Exchange mailboxes, referred to as the primary and secondary lab users in this lab.

Exercise 1: Working with Calendar Items

Task 1 – Beginning the Lab

In this task, you will open the project and configure it to run with your accounts.

1.  Navigate to Start All Programs Microsoft Visual Studio 2010.

2.  Click on the Microsoft Visual Studio 2010 icon to start Visual Studio 2010.

3.  Select File Open Project.

4.  Navigate to the folder C:\%Office365TrainingKit%\Labs\10.1\Source\Before\ EWSMA_Calendars.sln.

5.  Open the EWSMA_Calendars project.

6.  In Solution Explorer, open the app.config file.

7.  Change the PrimaryLabUserId and SecondaryLabUserId values to your primary and secondary lab accounts.

8.  Open the Program.cs file.

9.  Select View > Task List and select Comments from the menu.

Task 2 – Establishing a connection to the Exchange Server

In this task, you will create an ExchangeService object and connect to the correct Exchange CAS Server using your default credentials and your primary lab user ID.

1.  In the Task List, navigate to TODO: 10.1.1.

2.  Add the following code after the TODO: 10.1.1 comment. This creates the object reference to Exchange Web Services.

C#

private static ExchangeService _service;

3.  Navigate to TODO: 10.1.2.

4.  Add the following code after the TODO: 10.1.2 comment. This uses AutoDiscover to find the most efficient Client Access Server for the primary lab user’s mailbox. The UrlValidationCallback callback function validates the Urls that the AutoDiscover redirects the request to. In a development environment, you can implement a simple callback function that always returns true.

C#

_service = new ExchangeService(ExchangeVersion.Exchange2010);

_service.Credentials = new System.Net.NetworkCredential()

{

UserName = _primaryLabUserId,

Password = "pass@word1"

};

_service.AutodiscoverUrl(_primaryLabUserId, UrlValidationCallback);

Task 3 – Creating Items

In this task, you will create an appointment, set properties to add an attendee, set a recurrence pattern, save the appointment, and send the invitation.

1.  Navigate to TODO: 10.1.3.

2.  Add the following code after the TODO: 10.1.3 comment. This creates an Appointment, sets its basic properties, and sets an advanced property like the recurrence pattern.

C#

Appointment appointment = new Appointment(_service);

appointment.Subject = _companyName + " introduction to EWS";

appointment.Body = "Weekly status with " + _companyName;

appointment.RequiredAttendees.Add(_secondaryLabUserId);

appointment.Start = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.AddHours(1).Hour, 0, 0);

appointment.End = appointment.Start.AddHours(1);

DayOfTheWeek dayOfTheWeek = (DayOfTheWeek)Enum.Parse(

typeof(DayOfWeek), appointment.Start.DayOfWeek.ToString());

appointment.Recurrence = new Recurrence.WeeklyPattern(

appointment.Start.Date,

1, // Repeat every 1 week

dayOfTheWeek);

appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);

3.  Go to Debug > Start Without Debugging or use the shortcut key combination by pressing [CTRL]+[F5] to start the application.

4.  Press “1” to create a recurring calendar appointment.

5.  Open Microsoft Outlook 2010 and navigate to the Calendar.

6.  Verify that an appointment has been created an hour from the current time and that it recurs on the same day and time next week.

7.  Close the console application.

Task 4 – Finding Items

In this task, you will search the primary lab user’s calendar for appointments occurring in the next five days.

1.  Navigate to TODO: 10.1.4.

2.  Un-comment code surrounded by /* … */ block.

3.  Add the following code after the TODO: 10.1.4 comment. This searches for all appointments in the Calendar folder that fall into the date range specified by the CalendarView.

C#

CalendarView calendarView = new CalendarView(DateTime.Now,

DateTime.Now.AddDays(numberOfDays));

FindItemsResults<Appointment> appointments =

_service.FindAppointments(

WellKnownFolderName.Calendar,

calendarView);

4.  Go to Debug > Start Without Debugging or [CTRL]+[F5] to start the application.

5.  Press “2” to view the primary lab user’s appointments for the next five days.

6.  Return to Outlook 2010, and verify that all of the appointments in the next five days are displayed in the console.

7.  Close the console application.

Task 5 – Impersonation

Task Setup Requirements

In order to complete the this lab, you’ll need to grant the ApplicationImpersonation role to the primary lab user account over the secondary lab user account.

Open the Exchange Management Shell as an administrator on the Exchange Server and run the following command where -Name is a unique name for the role, -User is the primary lab user Id, and -RecipientOrganizationalUnitScope is the organizational unit containing the secondary lab user account.

New-ManagementRoleAssignment –Name “Impersonation-SeanChai” –Role “ApplicationImpersonation” –User sc –RecipientOrganizationalUnitScope fabrikam.com\OUs\Users\Sales

Follow this link for more information http://msdn.microsoft.com/en-us/library/bb204095.aspx

In this task, you will set the Exchange Service to impersonate the secondary lab user and create an appointment using their credentials.

1.  Navigate to TODO: 10.1.5.

2.  Un-comment code surrounded by /* … */ block.

3.  Add the following code after the TODO: 10.1.5 comment. This sets the ExchangeService to impersonate the secondary lab user, enabling you to perform operations against Exchange Server using the secondary lab user’s identity and permissions.

C#

_service.ImpersonatedUserId = new ImpersonatedUserId(

ConnectingIdType.SmtpAddress,

_secondaryLabUserId);

4.  Go to Debug > Start Without Debugging or [CTRL]+[F5] to start the application.

5.  Press “3” to impersonate the secondary lab user and create an appointment.

6.  Open Outlook 2010, open Calendar and verify that the primary lab user receives an appointment invitation from the secondary lab user.

7.  Close the console application.

8.  Close Outlook 2010.

Summary

In this lab, you used the EWS Managed API to build simple applications that leverage Exchange 2010 data and business logic. You saw how easy it is to get started with the EWS Managed API by showing the simplicity of connecting to the correct Exchange CAS server, working with mailbox items, finding mailbox items, and impersonating an Exchange user.