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:\%UC14TrainingKit%\Labs\5\Source\Before\GettingStartedwithEWSMA.sln.
  5. In the Solution Explorer, right-click the EWSMA_Calendars project and select Set as StartUp Project.
  6. Open the EWSMA_Calendars project.
  7. In Solution Explorer, open the app.config file.
  8. Change the PrimaryLabUserId and SecondaryLabUserId values to your primary and secondary lab accounts.
  9. Open the Program.cs file.
  10. 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: 5.1.1.
  2. Add the following code after the TODO: 5.1.1 comment. This creates the object reference to Exchange Web Services.

    C#

    private static ExchangeService _service;

  3. Navigate to TODO: 5.1.2.
  4. Add the following code after the TODO: 5.1.2 comment. This uses AutoDiscover to find the most efficient Client Access Server for the primary lab user’s mailbox.

    C#

    _service = new ExchangeService(ExchangeVersion.Exchange2010); _service.UseDefaultCredentials = true; _service.AutodiscoverUrl(_primaryLabUserId);

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: 5.1.3.
  2. Add the following code after the TODO: 5.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: 5.1.4.
  2. Un-comment code surrounded by /* … */ block.
  3. Add the following code after the TODO: 5.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

Note:
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 https://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: 5.1.5.
  2. Un-comment code surrounded by /* … */ block.
  3. Add the following code after the TODO: 5.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.