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