title: Publishing a custom presence message in OCS2007 R2 using UCMA 2.0 (visible in MOC)
body: <p>I'm writing a UCMA 2.0 application which connects with a 'UserEndpoint' to OCS. I want to be able to set a custom presence message along with my availability!</p>
  
  <p>I've run into some issues along the way and spent a lot of time Googling and tried out a few articles, the majority seem to be saying the same thing (including MS themselves):</p>
  
  <p>Use XML to set the 'userState' activity to be custom and contain a message, then publish that in a custom category via the endpoint's 'LocalOwnerPresence.BeginPublishPresence' method.</p>
  
  <p>I don't get any errors back from OCS when I do this (see the XML example below), but looking at the presence in Office Communicator (MOC) the user in question continues to appear offline. I have found that also publishing the 'machineState' means that we see some presence icon in MOC, but alas, I can't get the custom activity message working. </p>
  
  <p>More reading sent me on wild goose chases, such as stored procs in the OCS DB that need to be run to allow this (sounded more like for adding brand new presence categories rather than just a custom message to an existing state). I'm kind of at the end of my tether after a day 'online'.</p>
  
  <p>Any help or pointing out any gotchas would be appreciated! I've included a few of the sites I used when reading up on this:</p>
  
  <p>Presence Intro: <a href="http://msdn.microsoft.com/en-us/library/dd253506(v=office.13).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd253506(v=office.13).aspx</a></p>
  
  <p>Publishing Presence: <a href="http://msdn.microsoft.com/en-us/library/dd253494(v=office.13).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd253494(v=office.13).aspx</a></p>
  
  <p>Presence + Custom messages: <a href="http://blogs.claritycon.com/blog/2009/03/04/how-to-publish-presence-using-ucma-v2-0-and-have-a-clever-communicator-status/" rel="nofollow">http://blogs.claritycon.com/blog/2009/03/04/how-to-publish-presence-using-ucma-v2-0-and-have-a-clever-communicator-status/</a></p>
  
  <p>Machine state ideas: <a href="http://social.msdn.microsoft.com/Forums/en-US/ucmanagedsdk/thread/d3bd3569-66c0-4b52-86f0-900d2fb6d22e/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/ucmanagedsdk/thread/d3bd3569-66c0-4b52-86f0-900d2fb6d22e/</a></p>
  
  <p>Presence Schema doc: <a href="http://msdn.microsoft.com/en-us/library/dd941536(v=office.13).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd941536(v=office.13).aspx</a></p>
  
  <p>Here's the XML (string) that I'm using in my code, on the fly I string.Format the placeholders with a custom message and an availability integer value ("test message" and 3500 for the sake of argument).</p>
  
  <pre><code>string userStateXmlFormat = "&lt;state xmlns=\"http://schemas.microsoft.com/2006/09/sip/state\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" manual=\"true\" xsi:type=\"userState\"&gt;\n"
              + "&lt;availability&gt;{0}&lt;/availability&gt;\n"
                  + "&lt;activity&gt;\n" 
                     + "&lt;custom xmlns=\"http://schemas.microsoft.com/2006/09/sip/state\"&gt;{1}&lt;/custom&gt;\n" 
                  + "&lt;/activity&gt;\n"
              + "&lt;/state&gt;";
  </code></pre>
  
  <p>Here's the C# that does the formatting, and attempting to publish the presence:</p>
  
  <pre><code>var machineStateXml = string.Format(machineStateXmlFormat, 3500);
  var userStateXml = string.Format(userStateXmlFormat, 3500, "Test Message");
  var machineState = new CustomPresenceCategory("state", machineStateXml);
  var userState = new CustomPresenceCategory("state", userStateXml);                
  PresenceCategory[] categoriesToPublish = new PresenceCategory[] { machineState, userState };
  this.Endpoint.LocalOwnerPresence.BeginPublishPresence(categoriesToPublish, this.PublishPresenceCompleted, null);
  </code></pre>
  
  <p>Hopefully I haven't left anything out, please comment if you think there's something missing! </p>
  
  <p>Cheers
  Pete</p>