Using the Communicator Automation API

Recent Update to the Presence Controls:
http://www.microsoft.com/downloads/details.aspx?familyid=25A27453-15FF-48AB-AF1B-692CD77AB510&displaylang=en

I'm working on a small project which uses the Communicator Automation API. In fact I'm taking a short cut and a great head start by using the Sample Presence Controls.

If you are new to the Automation API, it isn't very intutive at all. The scenario I ran across was that I wanted to set the presence of the current user logged into Communicator.
Note: You can set the presence of the user logged in to Communicator, but you can not set someone else's presence.

I'm not a big fan of reading the documentation first.. I like to work with and figure it out. I'm the same way with the "some assembly required" items I buy. Ditch the instructions and figure it out.

Thinking that it should farily simple to set your own status, I start with declaring a Communicator object:

CommunicatorAPI.

MessengerClass communicatorObject = new CommunicatorAPI.MessengerClass();

Ok that is simple enough, now to set the presence status of the current user, I find the MyStatus property, which looks like it can be set with the MISTATUS Enum...

      communicatorObject.MyStatus = MISTATUS.MISTATUS_BUSY;

This seems logical syntax wise, it compiles and runs but .... it never sets "MY" Status. I sat an thought about the reasoning why this doesn't work. I mean if you are going to expose a property that isn't readonly and name the property "MyStatus", I should be able to set it, right?

The Solution

First you need to get the current logged in user in an object of type IMessengerContactAdvanced:

IMessengerContactAdvanced

contact = (IMessengerContactAdvanced)communicatorObject.GetContact(communicatorObject.MySigninName, communicatorObject.MyServiceId);

Next you need need to create an Object array, in which the index of the array determines which property to set. There are 8 properties we can set, so our Object array needs an initializer of 8. This isn't very coder friendly at all....

object[] presenceProperties = new object[8];

Next you need to set the first object in the array, index of 0, with our status.

presenceProperties[0] = MISTATUS.MISTATUS_BUSY;
or you can use the PresenceProperty Enum
presenceProperties[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = MISTATUS.MISTATUS_BUSY;

Finally add our Object array to the IMessengerContactAdvanced object.

contact.PresenceProperties = (

object)presenceProperties;

Final Solution

CommunicatorAPI.MessengerClass communicatorObject = new CommunicatorAPI.MessengerClass();

IMessengerContactAdvanced contact = (IMessengerContactAdvanced)communicatorObject.GetContact(communicatorObject.MySigninName, communicatorObject.MyServiceId);

object[] presenceProperties = new object[8];

presenceProperties[(

int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = (MISTATUS) status;

contact.PresenceProperties = (

object)presenceProperties;

I'm not sure of the WHY the the API was designed this way, but this is at least explains the HOW. For those of you who do read the documentation before implementing, MSDN does contain a lot of good documentation on the HOWs.
http://msdn2.microsoft.com/en-us/library/bb758774.aspx