Thursday, 26 July 2012

Adding appointments to an Outlook calendar using EWS (Exchange Web Services) in a Webpart

Recently I had a requirement to create a solution for tracking and scheduling internal events. As part of this, I wanted the solution to add and remove appointments from users Outlook calendars automatically upon registration / cancellation.

Exchange Web Services was the answer.

My approach was:

1. Generate "EWS.dll" (I named mine IEWS.dll)
2. Create a SharePoint solution that registers IEWS.dll as a safe control in the farm and contains an application page for managing my custom Exchange settings (usernames, urls, etc)
3. Create a second solution that contains the webparts, content types, etc, that my solution requires. This solution will have a reference to IEWS.dll, and feature dependency on the first solution.

Step 1.

Everyone seems to refer to EWS.dll on the Internet. The fact is, you need to generate it yourself, and you can call it whatever you like. There's a MSDN blog about it here.

The first step is creating a class file from the Exchange Web Services web service. I used the Microsoft wsdl.exe tool shipped with Visual Studio to do this. All you need to do is specify the language you want to use, the location the class file will be output to, the namespace you want to use (you specify whatever you want this to be) and the URL to an Exchange Web Services server (any of your Exchange servers running the Client Access role).

wsdl.exe /language:cs /out:c:\temp\IEws.cs /namespace:ExchangeWebServices https://myexchangeserver/ews/services.wsdl

Next, we need to compile the class and sign it, using the .Net framework 3.5. There are various ways to do this. What I did was: 

1. Create a new Visual Studio project (I named it IEWS - The "I" in IEWS is the prefix I used to denote our company )

2. Add a new class file to the project (I called mine ExchangeWebServices.cs, to match the class file name I created using the wsdl tool)

3. Delete the contents of the class file

4. Copy the contents of the class file you created with wsdl.exe (c:\temp\IEws.cs) into your new class file




5. Next I added some additional methods to handle my requirements. I added a new class called Extensions, and added my helper methods in there (adding and removing calendar entries, among others). You'll need to add two using statements, one for System.Web.Services, another for the ExchangeWebServices namespace, as well as any others you need, like Microsoft.SharePoint)

One of the my custom (overloaded) methods that adds a calendar appointment to a users mailbox looks like this:

public static Boolean CreateAppointment(string usersEmail, string subject, string description, string location, DateTime startTime, DateTime endTime, Guid appointmentUid, Credentials credentials, String exEwsUrl, out String messages)
{
  StringBuilder output = new StringBuilder();
  try
  {
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Credentials = new NetworkCredential(credentials.Username, credentials.Password.ConvertToUnsecureString(), credentials.Domain);
    esb.Url = exEwsUrl;
    esb.RequestServerVersionValue = new RequestServerVersion();
    esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

    //Setup Impersonation
    esb.ExchangeImpersonation = new ExchangeImpersonationType();
    esb.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType();
    esb.ExchangeImpersonation.ConnectingSID.PrimarySmtpAddress = usersEmail;


    // Create the request.
    AddDelegateType request = new AddDelegateType();
    //ToDp: handle the certificate check better.
    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

    // Create the appointment.
    CalendarItemType appointment = new CalendarItemType();

    // Add item properties to the appointment.
    appointment.Body = new BodyType();
    appointment.Body.BodyType1 = BodyTypeType.HTML;
    appointment.Body.Value = CreateHtmlBody(subject, description, location, startTime, endTime);
    appointment.Importance = ImportanceChoicesType.High;
    appointment.ImportanceSpecified = true;
    appointment.ItemClass = "IPM.Appointment";
    appointment.Subject = subject;
    appointment.Location = location;
    appointment.UID = appointmentUid.ToString();
    ExtendedPropertyType[] eProps = CreateAppointmentUidProperty(String.Format("{0}", appointmentUid));
    appointment.ExtendedProperty = eProps;

    // Add calendar properties to the appointment.
    var timeUtc = startTime.ToUniversalTime();
    var endTimeUtrc = endTime.ToUniversalTime();
    appointment.Start = timeUtc;
    appointment.StartSpecified = true;
    appointment.End = endTimeUtrc;
    appointment.EndSpecified = true;
    appointment.ReminderMinutesBeforeStart = "30";
    appointment.ReminderIsSet = true;

    
    // Identify the destination folder that will contain the appointment.
    var folder = new DistinguishedFolderIdType();
    folder.Id = DistinguishedFolderIdNameType.calendar;
    
    // Create the array of items that will contain the appointment.
    var arrayOfItems = new NonEmptyArrayOfAllItemsType();
    arrayOfItems.Items = new ItemType[1];

    // Add the appointment to the array of items.
    arrayOfItems.Items[0] = appointment;

    // Create the CreateItem request.
    CreateItemType createItemRequest = new CreateItemType();

    // The SendMeetingInvitations attribute is required for calendar items.
    createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
    createItemRequest.SendMeetingInvitationsSpecified = true;

    // Add the destination folder to the CreateItem request.
    createItemRequest.SavedItemFolderId = new TargetFolderIdType();
    createItemRequest.SavedItemFolderId.Item = folder;

    // Add the items to the CreateItem request.
    createItemRequest.Items = arrayOfItems;

    // Send the request and get the response.
    CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);

    ArrayOfResponseMessagesType responseMessages = createItemResponse.ResponseMessages;
    ResponseMessageType findResponseType = responseMessages.Items[0];
    if (findResponseType.ResponseClass != ResponseClassType.Success)
    {
      output.Append(String.Format("Failed to create item. Response Class: {0}. Response Messages: {1}", findResponseType.ResponseClass, findResponseType.MessageText));
      return false;
    }

    // Get the response messages.
    ResponseMessageType[] rmta = createItemResponse.ResponseMessages.Items;
    output.Append(String.Format("<div>An appointment has been added to your calendar.</div>"));
    output.Append(String.Format("<div>Repsonse:</div>"));
    foreach (ResponseMessageType rmt in rmta)
    {
      ArrayOfRealItemsType itemArray = ((ItemInfoResponseMessageType)rmt).Items;
      ItemType[] items = itemArray.Items;
      // Get the item identifier and change key for each item.
      foreach (ItemType item in items)
      {
        output.Append(String.Format("<div>Item identifier: {0}</div>", item.ItemId.Id));
        output.Append(String.Format("<div>Item change key: {0}</div>", item.ItemId.ChangeKey));
      }
    }
    return true;
  }
  catch (Exception e)
  {
    output.Append(e.Message);
    return false;
  }
  finally
  {
    messages = output.ToString();
  }
}

private static ExtendedPropertyType[] CreateAppointmentUidProperty(String value)
{
  PathToExtendedFieldType pathToAppointmentUid = new PathToExtendedFieldType();
  pathToAppointmentUid.DistinguishedPropertySetId = DistinguishedPropertySetType.PublicStrings;
  pathToAppointmentUid.DistinguishedPropertySetIdSpecified = true;
  pathToAppointmentUid.PropertyName = CalendarTrackingUidName;
  pathToAppointmentUid.PropertyType = MapiPropertyTypeType.String;

  ExtendedPropertyType appointmentPropertyUid = new ExtendedPropertyType();
  appointmentPropertyUid.ExtendedFieldURI = pathToAppointmentUid;
  appointmentPropertyUid.Item = value;

  ExtendedPropertyType[] eProps = new ExtendedPropertyType[1];
  eProps[0] = appointmentPropertyUid;
  return eProps;
} 

6. Once all the extension methods are finished, the next step is to sign the project and build it. This will, among other things, compile the code as IEWS.dll, which I can now use in my next project.

Step 2.

1. The next step I took, was creating a new empty SharePoint project (in the same solution). This project will be responsible for deploying the IEWS.dll to the SharePoint farm (so that other solutions can use it), and will install an application page I can use to configure settings the extension methods I created will need (usernames, passwords, URLs, etc).

To have the solution deploy IEWS.dll as an additional assembly when the solution is deployed;

1. Open the Package manager
2. Click on Advanced


3. Click Add > Add Assembly from Project Output...
4. For the Source Project, I selected the first project I created, IEWS, which contains the Exchange Web Services class and the extension class I created in Step 1.



5. Under the safe controls section, click on Click here to add new item
6. Add the ExchangeWebServices namespace


7. Click OK.
8. Build the solution and deploy it.

Step 3 - Make use of the Exchange Web Services in a webpart.

This is the easy bit!

1. Create a new solution, and add an empty SharePoint project

2. Add a reference to IEWS.dll created in the previous solution (note that this dll doesn't need to be packaged with this solution - the first solution is responsible for deploying it to your SharePoint servers)

3. Add a webpart to the project
4. Add all the controls you need to the webpart. Obviously this will depend on what you're doing. Mine looks like this;

4a. The webpart displays a list of available events a user can register for.

4b. When the user selects an event to register for, an application page is displayed in the SharePoint dialog framework to allow the user to register themselves or select someone else they are registering on behalf of.




5. When the user clicks Register, code adds the event to the specified users calendar using methods in my IEWS. The code in the application page looks a bit like this (I've slimmed it down to essentials)

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using IEWS;

namespace Ince.Events.Layouts.Ince.Events
{

public partial class bookevent : LayoutsPageBase
{
private Guid _listId;
private Guid _webId;
private int _itemId;

protected void Page_Load(object sender, EventArgs e){...}        

private void PopulateFields(Guid webid, Guid listid, int itemid){...}        

protected void SubmitClick(Object sender, EventArgs e)
{
try
{
(code to get page arguments)
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSite site = new SPSite(SPContext.Current.Site.Url))
{
site.AllowUnsafeUpdates = true;

using (SPWeb web = site.OpenWeb(webid))
{
try
{
web.AllowUnsafeUpdates = true;

(code omitted that gets the events list, gets the selected event, checks if there are available spaces, adds the selected user as an attendee and updates the list item)                                                                

//Add the event to the selected users mailbox
if (IEWS.ExchangeServices.CreateAppointment(usersemailaddress, item["Title"].ToString(), description.ToString(), locationAsText.ToString(), startDate, endDate, item.UniqueId))
{
eventConfirmationMessage.Text = String.Format("<div>You've been registered for this event, and we've added an appointment to your Outlook calendar to help remind you.</div>");
}
else
{
eventConfirmationMessage.Text = String.Format("<div>You've been registered for this event, but we failed to add an appointment to your Outlook calendar. Please make a note of the time and date, and optionally manually add the appointment to your Outlook calendar.</div>");
}
(code omitted that handles exceptions, etc)
...
}
6. Deploy the solution.

4 comments:

  1. Hi Matthew,

    Could you please help me in the extended properties? How did you implement the "CreateAppointmentUidProperty" method? (I'm trying to append extended properties into the calendaritem, but it's constantly null after creating the appointment).

    Thanks in advance!

    Imre

    ReplyDelete
    Replies
    1. Hi Imre,
      I added the CreateAppointmentUidProperty method into the example. Hope this helps.

      The classes this method refers to are from the ExchangeWebServices namespace, which was created the wsdl.exe tool (as mentioned at the top).

      Kind Regards, Matthew

      Delete
  2. Hi Matthew,

    Thanks for your help, it's working. :) I greatly appreciate your help!

    Best Regards,
    Imre

    ReplyDelete
  3. Great! Glad to hear it's working for you!

    ReplyDelete