You are currently browsing the monthly archive for September 2008.

During the past 2 weeks or so I’ve been experimenting with the Exchange Webservice (in Exchange 2007 SP1).

I’ve been trying to create a contact in the contacts folder of a user (testuser). I came accross some problems while trying to accomplish this. Specifically when I tried to set properties that are actually used by Outlook (client properties) and which are not necessarily used by Exchange 2007. But you can still access and even create properties all you want. Lets kick this blog off with the basics of working with the ews (Exchange WebService).

Some ground work:

  1. Create a new console app.
  2. Add the Web Reference to your Exchange Webservice (eg.: http://myMailServer/ews/Exchange.Asmx) and lets name it ExchangeService.
  3. Add the using to the ExchangeService

Now we can go and add a private member

private static ExchangeServiceBinding esb;

 

Initialize the ExchangeServiceBinding. You can insert credentials here in case of a https connection as seen in the example below.

esb = new ExchangeServiceBinding

{

    Credentials = new NetworkCredential

    {

        Domain = “example.com”,

        UserName = “testUser”,

        Password = “p455w0rd”

    },

    Url = https://192.168.2.143/EWS/Exchange.asmx”

};

 

esb.RequestServerVersionValue = new RequestServerVersion();

esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

 

As you can see in above code you need to set the server version, please do not ommit these 2 lines. If you do your code probably won’t work. In some cases you will get an error like this: 

System.Web.Services.Protocols.SoapException: The request is valid but does not specify the correct server version in the RequestServerVersion SOAP header. Ensure that the RequestServerVersion SOAP header is set with the correct RequestServerVersionValue.

Now that we have the base of the application we can start concentrating on creating a contact, so we’re going to create a method that does just that for us.

private static void CreateContact(string givenName, string surname)

{

    ContactItemType contact = new ContactItemType();

 

    contact.GivenName = givenName;

    contact.Surname = surname;

    contact.DisplayName = contact.Subject = givenName + ” “ + surname;

 

    contact.EmailAddresses = new EmailAddressDictionaryEntryType[1];

    EmailAddressDictionaryEntryType email1 = new EmailAddressDictionaryEntryType();

    email1.Key = EmailAddressKeyType.EmailAddress1;

    email1.Value = “testUser@example.com”;

    contact.EmailAddresses[0] = email1;

 

    CreateItemType createItem = new CreateItemType();

    createItem.Items = new NonEmptyArrayOfAllItemsType {Items = new[] {contact}};

 

    DistinguishedFolderIdType folder = new DistinguishedFolderIdType();

    folder.Id = DistinguishedFolderIdNameType.contacts;

 

    TargetFolderIdType targetFolder = new TargetFolderIdType();

    targetFolder.Item = folder;

 

    createItem.SavedItemFolderId = targetFolder;

 

    var response = esb.CreateItem(createItem);

}

 

You’ll see the contact being created in you testUser’s contacts folder.
If you don’t fill out the DisplayName, the contact will complain about it in outlook when you open the contact and then close it without editing anything. Outlook will try to save the contact item although we did not change anything. 

This was fairly easy to achieve, but just try to fill out all the contact’s porperties you need. You’ll soon find out that you need to use Extended Properties, because not all the properties are available on the ContactItemType type. I’ll show you how to add 2 Extended Properties. Firstly the Gender of the contact and secondly the Title. Insert following somewhere before the instantiation of the CreateItemType createItem.

 

    contact.ExtendedProperty = new ExtendedPropertyType[2];

 

    ExtendedPropertyType gender = new ExtendedPropertyType();

    gender.ExtendedFieldURI = new PathToExtendedFieldType();

    gender.ExtendedFieldURI.PropertyTag = “0x3a4d”;

    gender.ExtendedFieldURI.PropertyType = MapiPropertyTypeType.Short;

    gender.Item = “1”;

 

    ExtendedPropertyType title = new ExtendedPropertyType();

    title.ExtendedFieldURI = new PathToExtendedFieldType();

    title.ExtendedFieldURI.PropertyTag = “0x3a45”;

    title.ExtendedFieldURI.PropertyType = MapiPropertyTypeType.String;

    title.Item = “Dr.”;

 

    contact.ExtendedProperty[0] = gender;

    contact.ExtendedProperty[1] = title;

 

As you can see both these Extended Properties have a PropertyTag. This is not always the case. In other properties you’ll need to use the PropertyId and DistinguisedPropertyId, but we’ll see samples of that in a later post in which I am going to discuss how you need to create a DistributionList with the contact created here as the only member.

The Property Tags and Ids can be found in the pdf’s concerning the exchange protocol on msdn or through a nice tool that helped me alot these past weeks: OutlookSpy.

That’s it for now. I hope it was clear and helpfull in some way.
Until next time…