Thursday, August 28, 2008

Exchange 2007 Web Services (I): Create a calendar item with an Extended Property (CreateItem)

Here is my code sample to create an appointment item in Exchange 2007 throug Exchange Web Services (EWS), using the CreateItem method. It's commented, I hope that is sufficient to explain how to use this Exchange 2007 feature.

Also, in this sample, I explain how to add an Extended Property to our calendar item (in next post, I'll show you how to find this calendar item through this Extended Property):


public void CreateAppointment(DateTime fecha)
{
try
{
// Delegate to accept server certificate ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
};

// Init Web Service Instance
ExchangeServiceBinding ewsService = new ExchangeServiceBinding();

// Set credentials and URL
ewsService.Credentials = new NetworkCredential("User", "qwerty", "DOMAIN");
ewsService.Url = @"https://server.domain.com/ews/exchange.asmx";
// If we need pass through a proxy
ewsService.Proxy = new WebProxy("proxy.domain.com", true);
ewsService.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the appointment instance
CalendarItemType appointmentItem = new CalendarItemType();

// Set Start and End Properties
appointmentItem.Start = DateTime.Now.AddHours(-1);
appointmentItem.End = DateTime.Now.AddHours(1);

// Set Location
appointmentItem.Location = "Meeting point";

// Set the Subject
appointmentItem.Subject = "Talk about Exchange 2007";

// Set the Appointment's Body
appointmentItem.Body = new BodyType();
appointmentItem.Body.BodyType1 = BodyTypeType.Text;
appointmentItem.Body.Value = "Esto es una cita de prueba.";

// According to previous Exchange's versions
appointmentItem.ItemClass = "IPM.Appointment";

// We set an extra property
appointmentItem.DateTimeStamp = fecha;

// We set an Extended Property (NetShowUrl)
appointmentItem.ExtendedProperty = new ExtendedPropertyType[1];
appointmentItem.ExtendedProperty[0] = new ExtendedPropertyType();
PathToExtendedFieldType pathNetShowUrl = new PathToExtendedFieldType();
Guid mapiGuid = new Guid("{00062002-0000-0000-C000-000000000046}");
pathNetShowUrl.PropertySetId = mapiGuid.ToString ("D");
pathNetShowUrl.PropertyId = 0x8248;
pathNetShowUrl.PropertyIdSpecified = true;
pathNetShowUrl.PropertyType = MapiPropertyTypeType.String;
appointmentItem.ExtendedProperty[0].ExtendedFieldURI = pathNetShowUrl;
appointmentItem.ExtendedProperty[0].Item = "My NetShowUrl Property value";

// Initialize the instance to pass to the web service method
CreateItemType item = new CreateItemType();

// We add our appointment item to the parameters
item.Items = new NonEmptyArrayOfAllItemsType();
item.Items.Items = new ItemType[1];
item.Items.Items[0] = appointmentItem;

// Set the Calendary folder to save the item
DistinguishedFolderIdType calendarFolder = new DistinguishedFolderIdType();
calendarFolder.Id = DistinguishedFolderIdNameType.calendar;
calendarFolder.Mailbox = new EmailAddressType();
item.SavedItemFolderId = new TargetFolderIdType();
item.SavedItemFolderId.Item = calendarFolder;

// Set if we need to send to appointment's people and invitation
// (Required Property in CalendarItem)
item.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
item.SendMeetingInvitationsSpecified = true;

// Invoke the web service method
CreateItemResponseType response = ewsService.CreateItem(item);

// Get the response
ItemInfoResponseMessageType itemInfoResponse =
(ItemInfoResponseMessageType)response.ResponseMessages.Items[0];

// Check the result
if (itemInfoResponse.ResponseClass != ResponseClassType.Success)
{
// An error occurs
Console.Out.WriteLine("[CREATE ITEM RESULTS]");
Console.Out.WriteLine(itemInfoResponse.ResponseClass);
Console.Out.WriteLine(itemInfoResponse.ResponseCode);
Console.Out.WriteLine(itemInfoResponse.MessageText);
}
else
{
// No error
Console.Out.WriteLine("[CREATE ITEM RESULTS]");
Console.Out.WriteLine(itemInfoResponse.ResponseClass);
Console.Out.WriteLin(itemInfoResponse.ResponseCode);
}
}
catch (Exception ex)
{
// An unexpected error
string message = ex.Message;
Console.Out.WriteLine (string.Format("Unexpected error...{0}", message));
}
}

No comments: