Posted by: rcosic | 17/11/2011

Availability (presence) in Lync Client

Note: this post is part of Lync Client development series. You can find the following topics here:

This time, I will explain how presence state (availability and activity) can be handled by using Lync Client API.

There are several reasons you might want to handle it: you can publish it to some other application, or you can synchronize it with other application providing its own “availability” states. In either way, you have to know how availability is implemented in Lync and how it can be handled. Just to note, I wrote some of this topic in one of my recent posts, so please take a look on it also.

First of all, to get the availability status from Lync, you can use two approaches: subscribe to an event when this state is changed, and to call some methods on Lnyc API to find out this information. Let’s see this:

1) Subscribe to ContactInformationChanged event on Self object:

m_lyncClient.Self.Contact.ContactInformationChanged += new EventHandler(Self_ContactInformationChanged);

void Self_ContactInformationChanged(object sender, ContactInformationChangedEventArgs e)
{
// in case of signing out or in, self contact is already released
if (m_lyncClient.State == ClientState.SigningOut || m_lyncClient.State == ClientState.SignedOut || m_lyncClient.State == ClientState.SigningIn)
{
return;
}

Contact self = sender as Contact;

// has user changed his availability (therefore, his presence status)?
if (e.ChangedContactInformation.Contains(ContactInformationType.Availability))
{
//get actual contactModel availability (Will be int value within OCOM availaiblity ranges)
ContactAvailability availability = (ContactAvailability)self.GetContactInformation(ContactInformationType.Availability);

string activity = (string)self.GetContactInformation(ContactInformationType.Activity);

OnAvailabilityChanged(availability, activity);
}
}

2) Call Lync API methods to find out user’s availability:

internal ContactAvailability GetCurrentUserAvailability()
{
// Microsoft Lync Client is not even available
if (!Connected)
{
return ContactAvailability.Offline;
}

// in case of signing out or re-signing, self contact is already released
if (m_lyncClient.State == ClientState.SigningOut || m_lyncClient.State == ClientState.SignedOut || m_lyncClient.State == ClientState.SigningIn)
{
return ContactAvailability.Offline;
}

Contact self = m_lyncClient.Self.Contact;

//get actual contactModel availability (Will be int value within OCOM availaiblity ranges)
return (ContactAvailability)self.GetContactInformation(ContactInformationType.Availability);
}
Please note that here I check whether some Availability has changed for the currently logged-in user, and also I retrieve Activity state. This is necessary, especially if you want to track the exact state of user’s presence.

As you probably know, ContactAvailability is an enumeration which enlists the following values:

  • Invalid (-1),
  • None (0) – Do not use this enumerator. This flag indicates that the cotact state is unspecified.,
  • Free (3500) – A flag indicating that the contact is available,
  • FreeIdle (5000) – Contact is free but inactive,
  • Busy (6500) – A flag indicating that the contact is busy and inactive,
  • BusyIdle (7500) – Contact is busy but inactive,
  • DoNotDisturb (9500) – A flag indicating that the contact does not want to be disturbed,
  • TemporarilyAway (12500) – A flag indicating that the contact is temporarily away,
  • Away (15500) – A flag indicating that the contact is away,
  • Offline (18500) – A flag indicating that the contact is signed out.

But if you look at the presence states you can choose from the presence chooser combo inside the Lync Client, there are some other states:

  • Available,
  • Busy,
  • Do Not Disturb,
  • Be Right Back,
  • Off Work,
  • Appear Away.

So, which status corresponds to which, and can we synchronize them?

First, you should be aware of idle states. These states cannot be published, since they are set automatically by Lync Client. You can modify time needed to trigger these states inside the Options window. There are two idle states: Inactive and Away. Both do the same thing, only difference is time when they are automatically triggered.

Next, you can match those states which do an exact match, such as: Free (matches to Available), Busy (matches to Busy), DoNotDisturb (matches to Do Not Disturb), and Away (matches to Appear Away – but it is displayed as Away!).

So, what about the others? Well, for this purpose, you should use Activity state. The problem is, as stated in SDK’s documentation, it’s “A token describing current contact activity. Contact information item value type is String.” Unfortunatelly, you should match strings to be able to find out which status exactly is it. For example, Be Right Back state should resemble ContactAvailability of TemporarilyAway and Activity of “BeRightBack”; Off Work state should resemble ContactAvailability of Away and Activity of “OffWork”, etc.

All these present should be sufficient to be able to correctly set the user’s availability, as presented below:
internal void SetCurrentUserAvailability(ContactAvailability newState, string activityId)
{
// Microsoft Lync Client is not even available
if (Connected)
{
Dictionary publishData = new Dictionary();
publishData.Add(PublishableContactInformationType.Availability, newState);

if (!string.IsNullOrEmpty(activityId))
{
publishData.Add(PublishableContactInformationType.ActivityId, activityId);
}

object[] asyncState = { m_lyncClient.Self };

m_lyncClient.Self.BeginPublishContactInformation(publishData, null, asyncState);
}
}

I hope you find this post useful. Kind regards.

Advertisements

Responses

  1. Not clear how to determine “idle states” (Inactive, Away) from Lync client?

    • Hi Denis,
      you should search for FreeIdle availability change.

      Kind regards,
      Ratko.

  2. Thanks a lot for this post!! Really really usefull!!!!

  3. This article was a real saver rcosic…. Thanks
    But when I tried to change the location am getting a COM exception or the Location just shows in dropdown not in the main field….

    Any Idea on these?

  4. Cool post ! Thanks for, visiting this blog page mate. I shall message you again. I did not realise that.

  5. in the landing of Lync by SignInConfiguration.SignInAsAvailability set login status. How do I set the login status for the off-work? ContactAvailability enumeration does not have offwork state.Thanks !

    • Hi Patrick,
      You should set the Availability to “ContactAvailability.Away” and set the ActivityId to “Off-work”.

      Kind regards,
      Ratko.

  6. How to get the time for which user is idle or away using the Lync SDK? Lync client shows this information in parentheses such as (20 min away). Any idea how to get this value?

    • No clue. Please post the answer back if you find any. It will be most appreciated. Thank you.

      KR,
      Ratko.

  7. Really interesting stuff. I do want to do the same, but in Visual Basic. Do you know of somewhere I could get an example of the same thing?

  8. hi , how can we set the users presence to “be right back” , i tried to set it using the availability -> away and activityId-> Berightback ..it is not working ..any idea ..pls help

    • got it activityId should be Be-right-back …thankyou for the help provided , got this idea by reading your article ..

    • Hello Srinivas,

      no, but coding in Visual C# is quite similar to the Visual Basic (I code in both languages), so you should get yourself familiar with the basic language syntax and re-write the code I wrote with no pain.

      Kind regards,
      Ratko.

  9. can we verify the icon of the presence ex: available -> small circle green color …by any means ..i have seen a class AvailabilityIcon …but do not know the usage ..pls help ..

    • Hello Srinivas,

      I don’t really know what you mean. You can’t get an icon into the Lync API, but you can inspect the presence accordingly.

      Kind regards,
      Ratko.

  10. How to handle “In a call” status since this status wont be available in the drop down combobox that we create to show the available states which user can choose from?

    • Hello Ray,

      you should be able to check whether the user is in ‘automatic busy state’, such as “In a call” or “In a meeting” by getting the current user’s availability and activity:

      Contact self = m_lyncClient.Self.Contact;
      ContactAvailability availability = (ContactAvailability)self.GetContactInformation(ContactInformationType.Availability);
      string activity = (string)self.GetContactInformation(ContactInformationType.Activity);
      string activityId = (string)self.GetContactInformation(ContactInformationType.ActivityId);

      Then, afterwards, you can perform the following check:

      current.Availability == ContactAvailability.Busy && (
      string.Equals(current.ActivityId, “on-the-phone”, StringComparison.OrdinalIgnoreCase)
      || string.Equals(current.ActivityId, “in-a-conference”, StringComparison.OrdinalIgnoreCase)
      || string.Equals(current.ActivityId, “in-a-meeting”, StringComparison.OrdinalIgnoreCase))

      Activity Id’s are fixed string values which should correspond to the activity id received from the API.

      Kind regards,
      Ratko.

  11. […] I’ve found a great ressource here: https://rcosic.wordpress.com/2011/11/17/availability-presence-in-lync-client/ […]

  12. Thank you! This post is more clear than any of MS Lync SDK websites, You are a real star!!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: