Receiving Contacts Presence

The subscription to contacts and groups category instances provides a mechanism to maintain the identities of a user’s contacts and groups across different application sessions and to roam the list across multiple established endpoints. However, the contact list constructed this way does not specify any presence information about the contained contacts. In many applications scenarios, a meaningful contact list requires that presence and other information about a contact be displayed as well. In any Microsoft Lync Server 2010 deployment, the contacts’ presence information is obtained via a separate remote subscription to presence categories by the contacts in the contact list. The walkthrough presented in Receiving Enhanced Presence should guide you through on how to receive presence of each contact once the contact list is obtained.

As a convenience, the following code example is provided to illustrate how to manage the subscription to contacts presence. Specifically, it covers the programming tasks to subscribe and unsubscribe to the contacts presence, to refresh an ongoing subscription to the contacts presence, and to make on-demand query of the contacts presence. Initialization of this contacts presence subscription is discussed in Preparing to Receive the Contact List.

        #region Managing contacts' presence subscription

        /// <summary>
        /// Start to receive contacts presence publications. It is called when the specified 
        /// contacts are added to the contact list.
        /// </summary>
        /// <param name="contacts"></param>
        public void SubscribeToContactsPresence(
            System.Collections.ObjectModel.Collection<NotificationItem<Contact>> contacts)
        {
            List<RemotePresentitySubscriptionTarget> targets =
                new List<RemotePresentitySubscriptionTarget>();
            foreach (NotificationItem<Contact> contactItem in contacts)
            {
                targets.Add(new RemotePresentitySubscriptionTarget(contactItem.Item.Uri));
            }
            _cgPresenceView.StartSubscribingToPresentities(targets);
        }

        /// <summary>
        /// Stop receiving contacts presence publications. It is called when the specified contacts
        /// are deleted from the contact list.
        /// </summary>
        /// <param name="sipUris"></param>
        public void UnsubscribeToContactsPresence(string[] sipUris)
        {
            if (sipUris != null)
            {
                _cgPresenceView.StartUnsubscribingToPresentities(sipUris);
            }
        }

        public void UnsubscribeToContactsPresence()
        {
            System.Collections.ObjectModel.Collection<string> sipUris = _cgPresenceView.GetPresentities();
            if (sipUris != null)
            {
                _cgPresenceView.StartUnsubscribingToPresentities(sipUris);
            }
        }

        /// <summary>
        /// Refresh the presence subscription to the contacts.
        /// </summary>
        public void RefreshContactsPresence()
        {
            // Synchronizing an asynchronous call.
            _cgPresenceServices.EndRefreshRemotePresenceViews(
                _cgPresenceServices.BeginRefreshRemotePresenceViews(null, null));
        }

        /// <summary>
        /// Query the specified categories published by every contact in the contact list.
        /// </summary>
        /// <param name="categoryNames">Category names</param>
        public void QueryContactsPresence(string[] categoryNames)
        {
            System.Collections.ObjectModel.Collection<string> sipUris = 
                _cgPresenceView.GetPresentities();

            // Synchronizing an asynchronous call.
            _cgPresenceServices.EndPresenceQuery(
                _cgPresenceServices.BeginPresenceQuery(sipUris, categoryNames,
                     ContactPresenceNotificationReceivedEventHandler, null, null));
        }

        /// <summary>
        /// Query the specified categories published by the specified contacts
        /// </summary>
        /// <param name="sipUris">SIP URIs of the specified contacts</param>
        /// <param name="categoryNames">Category names</param>
        public void QueryContactsPresence(string[] sipUris, string[] categoryNames)
        {
            // Synchronizing an asynchronous call.
            _cgPresenceServices.EndPresenceQuery(
                _cgPresenceServices.BeginPresenceQuery(sipUris, categoryNames,
                     ContactPresenceNotificationReceivedEventHandler, null, null));
        }

        #endregion Managing contacts' presence subscription