Thursday, August 28, 2008

Exchange 2007 Web Services (II): Find a calendar item by an Extended Property (FindItem)

In my previous post, I explained how to create a calendar item in Exchange 2007 through Exchange Web Services (EWS) with an extended property (NetShowUrl).

In this post, I will show you, how to find this calendar item by this property.

Here is the commented code:


public CalendarItemType FindAppointment(String filterValue)
{
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;

// Set Exchange properties
ewsService.RequestServerVersionValue = new RequestServerVersion();
ewsService.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

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

// Set the Calendary folder to save the item
DistinguishedFolderIdType[] folders = new DistinguishedFolderIdType[1];
folders[0] = new DistinguishedFolderIdType();
folders[0].Id = DistinguishedFolderIdNameType.calendar;
// If we need to find the another's user item
//folders[0].Mailbox = new EmailAddressType();
//folders[0].Mailbox.EmailAddress = "user1@example.com";
item.ParentFolderIds = folders;

// Extended Property by we do the search
PathToExtendedFieldType netShowUrlPath = new PathToExtendedFieldType();
Guid mapiGuid = new Guid("{00062002-0000-0000-C000-000000000046}");
netShowUrlPath.PropertySetId = mapiGuid.ToString ("D");
netShowUrlPath.PropertyId = 0x8248; // NetShowUrl
netShowUrlPath.PropertyIdSpecified = true;
netShowUrlPath.PropertyType = MapiPropertyTypeType.String;

// To get this extra property to in our search
PathToUnindexedFieldType datetimestampPath = new PathToUnindexedFieldType();
datetimestampPath.FieldURI = UnindexedFieldURIType.calendarDateTimeStamp;

// We set wich properties we want
item.ItemShape = new ItemResponseShapeType();
item.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
item.ItemShape.AdditionalProperties = new BasePathToElementType[2];
item.ItemShape.AdditionalProperties[0] = netShowUrlPath;
item.ItemShape.AdditionalProperties[1] = datetimestampPath;

// The Filter
item.Restriction = new RestrictionType();
IsEqualToType filterNetShowUrl = new IsEqualToType();
filterNetShowUrl.FieldURIOrConstant = new FieldURIOrConstantType();
FieldURIOrConstantType netShowUrlConstanType = new FieldURIOrConstantType();
ConstantValueType netShowUrlConstantValueType = new ConstantValueType();
netShowUrlConstantValueType.Value = filterValue;
netShowUrlConstanType.Item = netShowUrlConstantValueType;
filterNetShowUrl.Item = netShowUrlPath;
filterNetShowUrl.FieldURIOrConstant = netShowUrlConstanType;
item.Restriction.Item = filterNetShowUrl;

// Invoke the web service method
FindItemResponseType response = ewsService.FindItem (item);

// Get the response
FindItemResponseMessageType findItemResponse =
(FindItemResponseMessageType)response.ResponseMessages.Items[0];

// Check the result
if (findItemResponse.ResponseClass != ResponseClassType.Success)
{
// An error occurs
Console.Out.WriteLine("[FIND ITEM RESULTS]");
Console.Out.WriteLine(findItemResponse.ResponseClass);
Console.Out.WriteLine(findItemResponse.ResponseCode);
Console.Out.WriteLine(findItemResponse.MessageText);

return null;
}
else
{
// Get the first item
ArrayOfRealItemsType arrayItems = (ArrayOfRealItemsType)findItemResponse.RootFolder.Item;
CalendarItemType appointmentItem = (CalendarItemType)arrayItems.Items[0];

// Print the item's info
Console.Out.WriteLine("[FIND ITEM RESULTS]");
Console.Out.WriteLine(appointmentItem.Start);
Console.Out.WriteLine(appointmentItem.End);
Console.Out.WriteLine(appointmentItem.Subject);
Console.Out.WriteLine(appointmentItem.Location);
Console.Out.WriteLine(appointmentItem.DateTimeStamp.ToUniversalTime().ToString());

// Return the Item
return appointmentItem;
}
}
catch (Exception ex)
{
// An unexpected error
string message = ex.Message;
Console.Out.WriteLine(string.Format("Unexpected error...{0}", message));
return null;
}
}

No comments: