Search code examples
c#skype-for-businessskypedeveloperucmaucwa

Getting User Presence using UCMA?


I just need to give an email address and have the ucma protocol send back the user's presence from skype for business using C#.

Does anyone have some tips on how to obtain the user presence in ucma? The tutorials on msdn are not clear on this.

This is my current code

List<string> targets = new List<string>();
targets.Add("sip:[email protected]");
_endpoint.PresenceServices.BeginPresenceQuery(targets, new string[] {"state"}, null, ar =>
{
    List<RemotePresentityNotification> presence = new List<RemotePresentityNotification>(_endpoint.PresenceServices.EndPresenceQuery(ar));
    foreach (RemotePresentityNotification rpn in presence)
    {
         Console.WriteLine(rpn.PresentityUri + " is " + rpn.AggregatedPresenceState.EndpointLocation);
    }
}, null);

Solution

  • Your code looks fine. The only think I would say is that it's returning a list of presence state for all endpoints registered for the sip address. What you would normally want to use is the aggregated presence which is the overall presence used and displayed by all the skype applications.

    So you would normally use:

    • presence.AggregatedPresenceState.Availability is the base availability
    • presence.AggregatedPresenceState.Activity.ActivityToken is the well known sub-state of the base availability like "in-a-meeting", "on-the-phone", etc.
    • presence.AggregatedPresenceState.Activity.CustomTokens is a list of locale strings of custom presence states as it normally defined by the custom presence xml setup in skype for the user.

    There are lots of other information in there as well including all the other information the user can set like location (EndpointLocation) and note (PersonalNote).

    Also what is usage is the queryResultHandler parameter (you pass in null in your example, which is fine). This is called when the presence result becomes available. This is useful when you give it the query a list of sip addresses of more than one. The userCallback is called when all the presence results have come in (i.e. the whole operation is complete), which can take awhile and any failures will fail the whole call on the call to EndPresenceQuery. The queryResultHandler will be called multiple times when each successful presence result comes in, so you get your results faster and you see all the successful results even though the some may fail and cause the EndPresenceQuery to throw.

    Beyond that you can also subscribe to a endpoint for presence changes if you want to be kept up to date on when a endpoint changes presence. You have to be careful about the subscription as there are limits on how many a endpoint can have subscribed to them. This is where you get into the subscription types of Polling and Subscribed. Depending on the type of application you are writing depends on what type of subscription you need. Typically you only want to subscribe to endpoints what you are displaying/using live at the time.