Share via


Querying Presence of Specified Presentities

In Microsoft Unified Communications Managed API (UCMA) 3.0, querying presence is performed using an LocalEndpointPresenceServices object that can be obtained from an established (or connected) LocalEndpoint object.

Querying presence is an asynchronous process. Each query corresponds to a single SUBSCRIBE request submitted by or on behalf of the user. A client starts a query by using the BeginPresenceQuery/EndPresenceQuery pattern supported by the LocalEndpointPresenceServices object. The query results are returned by invoking the event handler that is passed in to the call to BeginPresenceQuery as the queryResultHandler parameter.

The following code example shows how to query specified categories published by given presentities.

        #region Querying remote presence
        public void QueryRemotePresence(string[] targets, string[] categories)
        {
            QueryRemotePresence(targets, categories, QueryingPresenceReceivedEventHandler);
        }

        public void QueryRemotePresence(string[] presentityUris, string[] categoryNames, 
            EventHandler<RemotePresentitiesNotificationEventArgs> queryResultHandler)
        {
            _remotePresenceServices.BeginPresenceQuery(
                presentityUris, 
                categoryNames, 
                queryResultHandler,
                CallbackOnBeginPresenceQueryReturned, 
                _remotePresenceServices);
        }

        private void CallbackOnBeginPresenceQueryReturned(IAsyncResult result)
        {
            try
            {
                if (_remotePresenceServices == result.AsyncState as LocalEndpointPresenceServices)
                {
                    _remotePresenceServices.EndPresenceQuery(result);
                    // inform the caller of the OK status of the PresenceQuery operation.
                    if (OnQueryPresenceCompleted != null)
                        RaiseEvent(OnQueryPresenceCompleted, this,
                            new AsyncOpStatusEventArgs(AsyncOpStatus.OK, null));
                }
            }
            catch (Exception ex)
            {
                // inform the caller of the Error status of the presence querying operation.
                if (OnQueryPresenceCompleted != null)
                    RaiseEvent(OnQueryPresenceCompleted, this,
                        new AsyncOpStatusEventArgs(AsyncOpStatus.Error, ex));
            }
        }
        #endregion Querying remote presence

In this example, the query results are returned in the QueryingPresenceReceivedEventHandler handler. Its implementation is listed in Preparing to Receive Presence Publications.