Thursday, August 28, 2008

Exchange 2007 Web Services (III): Update a calendar item (UpdateItem)

In my previous post, I explained how create a calendar item with an extended property and how find this calendar item by the extended property.

In this post, I will explain how to update this calendar item, modifying the appointment's End datetime.

This is the commented code:


public void UpdateAppointment(CalendarItemType calendarItem)
{
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;

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

// Initialize the instance to pass to the web service method
UpdateItemType updateItem = new UpdateItemType();
updateItem.ItemChanges = new ItemChangeType[1];
updateItem.ItemChanges[0] = new ItemChangeType();

// Set the Calendar Item's ID (the item wich will be updated)
updateItem.ItemChanges[0].Item = calendarItem.ItemId;
updateItem.ItemChanges[0].Updates = new ItemChangeDescriptionType[1];

// The instance to modify the End property
SetItemFieldType setCalendarEnd = new SetItemFieldType();

// The path to the End property
PathToUnindexedFieldType calendarEndPath = new PathToUnindexedFieldType();
calendarEndPath.FieldURI = UnindexedFieldURIType.calendarEnd;

// The new End property's value
CalendarItemType calendarEndItem = new CalendarItemType();
calendarEndItem.End = new DateTime(2008, 08, 28, 17, 00, 00);
// If no -> ErrorIncorrectUpdatePropertyCount
calendarEndItem.EndSpecified = true;

setCalendarEnd.Item = calendarEndPath;
setCalendarEnd.Item1 = calendarEndItem;

updateItem.ItemChanges[0].Updates[0] = setCalendarEnd;

// This property is required in the UpdateItem method, when we update calendar items
updateItem.SendMeetingInvitationsOrCancellations = CalendarItemUpdateOperationType.SendToNone;
updateItem.SendMeetingInvitationsOrCancellationsSpecified = true;

// Invoke the web service method
UpdateItemResponseType response = ewsService.UpdateItem(updateItem);

// Get the response
UpdateItemResponseMessageType updateItemResponse =
(UpdateItemResponseMessageType)response.ResponseMessages.Items[0];

// Check the result
if (updateItemResponse.ResponseClass != ResponseClassType.Success)
{
// An error occurs
Console.Out.WriteLine("[UPDATE ITEM RESULTS]");
Console.Out.WriteLine(updateItemResponse.ResponseClass);
Console.Out.WriteLine(updateItemResponse.ResponseCode);
Console.Out.WriteLine(updateItemResponse.MessageText);
}
else
{
// No error
Console.Out.WriteLine("[UPDATE ITEM RESULTS]");
Console.Out.WriteLine(updateItemResponse.ResponseClass);
Console.Out.WriteLine(updateItemResponse.ResponseCode);
}
}
catch (Exception ex)
{
// An unexpected error
string message = ex.Message;
Console.Out.WriteLine(string.Format("Unexpected error...{0}", message));
}
}
Pay attention to this line:

calendarEndItem.End = new DateTime(2008, 08, 28, 17, 00, 00);
calendarEndItem.EndSpecified = true;

If we don't set EndSpecified property, we will receive a confusing error code: ErrorIncorrectUpdatePropertyCount.

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;
}
}

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));
}
}

Thursday, August 21, 2008

New version of NDoc (NDoc3)

After NDoc, a new version of this code documentation generation tool is developed to support C# 2.0 and 3.0, is NDoc3.

At this moment, is a Beta version, but it works with a few bugs.

The project page is http://sourceforge.net/projects/ndoc3/.

Tuesday, August 19, 2008

Map Error - The map contains a reference to a schema node that is not valid

Yesterday, developing a map, I received this error:


Exception Caught: The map contains a reference to a schema node that is not valid.
Perhaps the schema has changed.
Try reloading the map in the BizTalk Mapper.
The XSD XPath of the node is:


The problem is including or importing Schemas in other Schema. I don't know the reason, I'm studiyng this error, when I have an explanation, I will write a new post with the fix.

Thursday, August 07, 2008

Publishing Orchestrations/Schemas as Web Services: "[System.Xml.Schema.XmlSchemaException] The content type of the base type must not be a simple..."

If we publish an Orchestration/Schema as Web Service and receive:

Unable to load assembly. ''[System.Xml.Schema.XmlSchemaException] The content type of the base type must not be a simple type definition.


If we are publishing schemas, we will have to validate the schema.

If we are publishing orchestrations, we will have to validate the schemas of the messages that we receive or send by the orchestration ports and validate that the xsd are correct.

We can check our XSDs at W3C.

BizTalk Mapper - Specific Namespace Prefix

By default, BizTalk Maps generates the output xml files with ns0, ns1, ns2, ... namespace prefix. If we need to set this prefixes to a specific value, we will have to use the map's "Custom XSL Path" property by following these steps:

1) We validate our current Map (the original map, that generates ns0, ... prefixes) -> Right Click on Map File and select "Validate Map".

2) We get the generated XSL file. The path is in the Results Panel:
The output XSLT is stored in the following file: <file:///C:\Documents and Settings\F1504\Configuración local\Temp\_MapData\CsvEntrada2XmlCache.xsl>

3) We change the namespace values in the generated XSL file, finding and replacing "ns0:" to whatever we want.

4) We add a new map to our Project, with source and destination schemas the same as out original map.

5) We set "Custom XSL Path" property in our new map, selecting the XSLT file that previously we have modified.