Note: this post is part of Lync Client development series. You can find the following topics here:
- Microsoft Lync Client Development,
- Sign in and out in Microsoft Lync Client,
- Lync Controls inside Lync Client SDK,
- Starting with Lync Client’s API,
- Handling some common scenarios with Lync Conversation,
- Handling transfers in Lync Client API,
- Handling conference calls in Lync Client,
- Availability (presence) in Lync Client.
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.
Not clear how to determine “idle states” (Inactive, Away) from Lync client?
By: Denis on 22/12/2011
at 08:24
Hi Denis,
you should search for FreeIdle availability change.
Kind regards,
Ratko.
By: rcosic on 16/03/2012
at 08:36
Thanks a lot for this post!! Really really usefull!!!!
By: Daniel on 14/03/2012
at 17:34
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?
By: SabKri on 20/04/2012
at 05:46
Cool post ! Thanks for, visiting this blog page mate. I shall message you again. I did not realise that.
By: source on 04/06/2012
at 09:40
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 !
By: patrickpeng on 15/06/2012
at 04:14
Hi Patrick,
You should set the Availability to “ContactAvailability.Away” and set the ActivityId to “Off-work”.
Kind regards,
Ratko.
By: rcosic on 20/08/2012
at 12:14
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?
By: rajeshad on 20/11/2012
at 08:22
No clue. Please post the answer back if you find any. It will be most appreciated. Thank you.
KR,
Ratko.
By: rcosic on 13/12/2012
at 09:12
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?
By: David on 16/03/2013
at 16:48
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
By: Srinivas on 23/08/2013
at 13:14
got it activityId should be Be-right-back …thankyou for the help provided , got this idea by reading your article ..
By: Srinivas on 23/08/2013
at 13:16
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.
By: rcosic on 25/11/2016
at 07:47
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 ..
By: Srinivas on 23/08/2013
at 13:31
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.
By: rcosic on 25/11/2016
at 07:49
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?
By: Ray on 07/01/2015
at 11:48
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.
By: rcosic on 25/11/2016
at 07:54
[…] I’ve found a great ressource here: https://rcosic.wordpress.com/2011/11/17/availability-presence-in-lync-client/ […]
By: Lync Client SDK : Receiving sometimes weird ContactAvailability - BlogoSfera on 21/09/2015
at 20:18
Thank you! This post is more clear than any of MS Lync SDK websites, You are a real star!!!
By: Zodiac on 24/12/2015
at 22:10