none
How to set Presence programmatically? please help

    Question

  •  

    Hello:

     

    I'd like to set Presence status programmatically, but out of all MISTATUS enum only 4 can be set, (Away, BeRightBack, Busy and Online), the rest all generate an error:

     

    System.ArgumentException: {"Value does not fall within the expected range."}

     

    So I can't change the status to, say, MISTATUS_DO_NOT_DISTURB

     

    Please advise,

    Thank you

    Wednesday, May 07, 2008 4:04 PM

Answers

  • I assume that you're trying to set the status of the currently signed in user, not any other user on the contact list...

     

    Here's a little function I use to set the current user's status:

     

    Code Snippet

    public static void SetMyStatus(MISTATUS status)

    {

    try

    {

    IMessengerContactAdvanced myContact = (IMessengerContactAdvanced)

    _communicator.GetContact(_communicator.MySigninName, String.Empty);

    object[] mPP = new object[8]; // There are 8 default presence properties ...

    mPP[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = status;

    myContact.PresenceProperties = (object)mPP;

    }

    catch (Exception e)

    {

    throw (e);

    }

    }

     

     

    Thursday, May 08, 2008 3:05 PM

All replies

  •  

    1 question - is yoru application only leveraging text chat functionalities or are you also making use of Audio chat

     

    secondly what is yoru client operating system?

    Wednesday, May 07, 2008 5:33 PM
  • Hello,

     

    my application does not using any of chat functionalities, it only provides a dropdown list for user to select and subsequently changes the Presence.  dropdown list items can be 'Away', 'Busy', 'Do not disturbe', etc.

     

    OS is WinXP SP2

     

    please help,

    thank you

    Wednesday, May 07, 2008 6:41 PM
  • I assume that you're trying to set the status of the currently signed in user, not any other user on the contact list...

     

    Here's a little function I use to set the current user's status:

     

    Code Snippet

    public static void SetMyStatus(MISTATUS status)

    {

    try

    {

    IMessengerContactAdvanced myContact = (IMessengerContactAdvanced)

    _communicator.GetContact(_communicator.MySigninName, String.Empty);

    object[] mPP = new object[8]; // There are 8 default presence properties ...

    mPP[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = status;

    myContact.PresenceProperties = (object)mPP;

    }

    catch (Exception e)

    {

    throw (e);

    }

    }

     

     

    Thursday, May 08, 2008 3:05 PM
  • Thank you George:

     

    Yes, you are right, i am changing my own presence status.

     

    might i know the difference between PRESENCE_PROP_MSTATE and PRESENCE_PROP_AVAILABILITY?

     

    greatly appreciate your input,

    thank you

     

     

    Thursday, May 08, 2008 6:54 PM
  • Think of the concept of presence as having two dimensions: availability and willingness. For example, if your status is set to Do Not Disturb, you are technically available, but unwilling.

     

    The availability that people see for a contact is also dependent on their level of access. For example, if you are in my company - your level or access for me would be set to Company by default. Say I'm in a meeting, you would see my status/availability as Busy/In a Meeting. If I were on the phone, you would see Busy/In a Call.

     

    However, if you were one of my external contacts, you would only see me as Busy.

     

    So, PRESENCE_PROP_MSTATE represents Status, while PRESENCE_PROP_AVAILABILITY represents availability. Each is queried differently:

     

    In my opinion Availability is only relevant when in the context of Status.

     

    Code Snippet

    Object[] cProperties = contact.PresenceProperties as Object[];

    availability = (int)cProperties[(int)PRESENCE_PROPERTY.PRESENCE_PROP_AVAILABILITY];

    presenceStatus = (MISTATUS)cProperties[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE];

     

     

    Take a look at the Office Communicator 2007 WinForms Presence Controls sample that is available, there's some great sample code in there.
    Tuesday, May 13, 2008 1:15 PM
  • Thank you George, much appreciate your input, very helpfule.

     

    also, i'd like confirm with you that you cannot set the availability, sometimes is because server admin has disabled the feature.  for example, if i want to set my status as "Appear off line" programmatically, it generates error.  is this because server admin has disabled the ability for you to set that status?

     

    Thank you,

    Wednesday, May 14, 2008 3:49 PM
  • I've never tried to set Availability programmatically. I'm not familiar with the administrative side of things, so I don't know if that's controlled by some setting.

     

    It does make some sense though, for example it wouldn't make sense to programmatically set my availability to "In a Meeting". It seems like it's dynamically set based on Status and other contextual information.

    Wednesday, May 14, 2008 8:53 PM
  • Can anybody explain to me why the following event handler for ContactStatusChange always throws me an exception with the message "Value does not fall within the expected range.".  I am using the Office Communicator Automation API with OCS 2007 to develop a plugin.

     

    Thanks,

     

    Simon

     

    void OnContactStatusChange(object pMContact, MISTATUS mStatus)

    {

      try

      {

        IMessengerContactAdvanced imca = pMContact as IMessengerContactAdvanced;

        if ((imca != null&& (imca.FriendlyName == m_Communicator.MyFriendlyName))

          {

          object[] arPres = imca.PresenceProperties as object[];

          arPres[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = (int)MISTATUS.MISTATUS_CUSTOM; 

          arPres[(int)PRESENCE_PROPERTY.PRESENCE_PROP_CUSTOM_STATUS_STRING] = @"Playing Halo 3";

          imca.PresenceProperties = (object)arPres;

        }

      }

      catch (Exception ex)

      {

        Console.Writeln("execption setting presence {0}", ex.Message);

      }

    }

    Tuesday, June 17, 2008 11:31 AM
  • Hi SimonShaw

    Did you find out why  "Value does not fall within the expected range."?

     

    I am trying to set the PRESENCE_PROP_PRESENCE_NOTE but I am getting the same error. Other Presence Properties I can set OK with error.

     

    Thanks.

     

    Sunday, August 10, 2008 1:27 AM
  • Hmm, this isn't the solution I was hoping for but you can ONLY change it to a CUSTOM STATE which is defined in Communicator by specifying the custom presence XML in your Communicator policy. I wrote it down in a blogpost: http://blogs.microsoft.nl/blogs/mhoekstra/archive/2009/12/29/setting-custom-presence-state-with-office-communicator-2007-r2-automation-api.aspx

    There is a way to get this code working but not exactly I was hoping for. It is possible to extend Office Communicator with custom presence status. You need a XML file available through HTTPS (this changed from Office Communicato 2007 to 2007 R2, before R2 you could have a local file as well). The file below is placed at https://www.hoekstraonline.net/CustomPresence.xml

    <customStates>
      <customState ID="1" availability="online">
        <activity LCID="1033">Out and about - use mobile</activity>
      </customState>
      <customState ID="2" availability="Busy">
        <activity LCID="1033">Down in the lab - use mobile</activity>
      </customState>
      <customState ID="3" availability="busy">
        <activity LCID="1033">Reviewing program documents</activity>
      </customState>
      <customState ID="4" availability="do-not-disturb">
        <activity LCID="1033">Executive Briefing with Customer</activity>
      </customState>
    </customStates>

    You need to change the registry to make Communicator read this file:

    Add a  String Value to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Communicator called CustomStateURL with in my example the value  https://www.hoekstraonline.net/CustomPresence.xml  (create the key Communicator when it doesn’t exist). When you restart Communicator you will see the following list with states:

    image

    Now when I change the code to the lines below it will not fail and successfully change the status to the custom text:

    // Publish custom activity
    mPP[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = MISTATUS.MISTATUS_CUSTOM;
    mPP[(int)PRESENCE_PROPERTY.PRESENCE_PROP_CUSTOM_STATUS_STRING] = "Executive Briefing with Customer";
    a_contact.PresenceProperties = (object)mPP;

    So unfortunately the behavior of setting the custom status only allows changing it in code to the predefined custom states. In my case that’s isn’t good enough because I don’t want to specify all possible locations in the Netherlands in the custom XML file but want to change it dynamically.

    The solution for this is writing a UCC API or UCMA API based application which will do this for me. I have some samples ready but the hard part is you have to create new endpoints to publish the custom status and keep it working when users close laptops, change networks, work offline and online again etc. I didn’t write a robust enough UCC or UCMA application to make this happen. Writing against the OC Automation API would really helped me here. Hopefully it’s changed in the Wave 14 API of Communicator.

    Tuesday, December 29, 2009 12:06 AM
  • Hi Simon,

    I am also facing the same problem as you have faced. Please let me know how did you solve the problem?

    I managed successfully to add a button to the conversation window in communicator's GUI using the registry hacks explained in http://technet.microsoft.com/en-us/library/bb963931.aspx.  When the button is pressed my application is opened and I can create an object by calling the CommunicatorAPI.Messenger() constructor which I can then use to get a list to all the contacts in my roster, so far so good.

    What I would like to do is to gain access to the IMessengerConversationWnd of the conversation window where the button was clicked so that I can then send a custom invite message to all participants.

    How do I get access to the relevant object without having a background application running that is registered on the OnIMWindowCreated event?

     

     

     

    Thanks,

    Francisco Fernandes

    Monday, May 17, 2010 9:00 AM
  • I assume that you're trying to set the status of the currently signed in user, not any other user on the contact list...

     

    Here's a little function I use to set the current user's status:

     

     

    Code Snippet

    public static void SetMyStatus(MISTATUS status)

    {

    try

    {

    IMessengerContactAdvanced myContact = (IMessengerContactAdvanced)

    _communicator.GetContact(_communicator.MySigninName, String.Empty);

    object[] mPP = new object[8]; // There are 8 default presence properties ...

    mPP[(int)PRESENCE_PROPERTY.PRESENCE_PROP_MSTATE] = status;

    myContact.PresenceProperties = (object)mPP;

    }

    catch (Exception e)

    {

    throw (e);

    }

    }

     

     

     

     


    I'm new here, The sample you provided is very useful.
    Sunday, January 16, 2011 12:25 AM
  • How can this be accomplished using Office Communicator 2005?

    PRESENCE_PROPERTY isn't available (2007 only) and when getting status of user "Out of Office" the returned status is MISTATUS_AWAY instead of MISTATUS_OUT_OF_OFFICE

    Wednesday, January 19, 2011 7:08 PM