How to: Show a list of persistent chat room participants

[This is preliminary documentation and is subject to change.]

This topic shows how to get a list of the active participants in a room and then keep the list up-to-date as participating users sign out of Microsoft Lync 2013 Preview and more users start to participate.

Note Note

Chat room membership differs from room participation. A user who is a member of a chat room has permission to participate in a chat room by posting messages and reading messages. A user cannot get a list of active chat room participants until the user is participating in the chat room. The chat room membership list is persistent while a chat room participant list is dynamic and changes as a chat room member signs in to and out of Lync 2013 Preview.

Chat room membership cannot be changed by using the Microsoft Lync 2013 Preview API. Instead, you must use an application that implements the Microsoft Lync Server 2013 Preview Persistent Chat SDK or the Persistent Chat Server Administration Tool. For information about using the tool, see Using Chat Admin Tool.

Before you complete the steps in this topic, your application must declare and initialize a Microsoft.Lync.Model.LyncClient instance, and the Lync client must be signed in. For more information, see How to: Sign In to Lync.

You must also have an instance of Microsoft.Lync.Model.Room.Room where the user is a member. For information about getting a room, see How to: Get persistent chat rooms. Your application must declare a list to display room participants. You cannot use Lync 2013 Preview API to add or remove a user from the membership list of a room.

Code example

The following example references the namespaces that are used in the following Windows Forms examples.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Room;
using System.Collections;

The following example declares the class fields that are referenced in the following sections of this topic.

        /// <summary>
        /// The LyncClient class instance that encapsulates the Lync client platform
        /// </summary>
        private LyncClient _client=null;

        /// <summary>
        /// The Room instance that a user joins from the list of rooms that are queried, searched for, or gotten from the followed rooms list
        /// </summary>
        private Room _room = null;

        /// <summary>
        /// The Uri of the local user who can post a message to a chat room
        /// </summary>
        private string _senderUri = "";

        


When you have obtained the Microsoft.Lync.Model.Room.Room instance selected by a user, register for a set of chat room events so that you can receive notification of chat room participant roster changes.

To register for Room events

  1. Un-register for events on the previously selected room that is referenced by the _room class field.

  2. Set the _room class field to the newly selected chat room.

  3. Register for chat room events.

  4. Fill a UI list with the names of active chat room participants.

The following example registers for chat room participant roster events and gets the chat room participant roster.

        /// <summary>
        /// Registers for message, state, and participant related events on a room.
        /// </summary>
        private void FillRoomProperties(Room room)
        {
            if (_room != null)
            {
                //un-Register for the events on the previous selected room.
                _room.ParticipantAdded -= room_ParticipantAdded;

                //un-Register for the events on the previous selected room.
                _room.ParticipantRemoved -= room_ParticipantRemoved;

            }


            _room = room;

            //Register for the event raised when a user joins the room.
            _room.ParticipantAdded += new EventHandler<RoomParticipantsEventArgs>(room_ParticipantAdded);

            //Register for the event raised when a user leaves the room.
            _room.ParticipantRemoved += new EventHandler<RoomParticipantsEventArgs>(room_ParticipantRemoved);

            FillRoomRoster(_room);



        }


The Room.Participants property returns the current list of active chat room participants. Use this property to fill a list of participant names. The IList<Microsoft.Lync.Model.Room.RoomUser> type is returned by the property. The RoomUser.Uri property returns a participant’s Sip address as a string. To get the participant display name, complete the following tasks with each Microsoft.Lync.Model.Room.RoomUser in the list:

To fill the participant list with participant names

  1. Verify that the RoomUser.Uri property is not null.

  2. Call the ContactManager.GetContactByUri method and pass the Sip address in the first argument. An instance of Microsoft.Lync.Model.Contact is returned.

  3. Get the display name of the participant by calling the Contact.GetContactInformation method, passing the ContactInformationType.DisplayName enumerator. The display name of the contact is returned.

  4. Add the participant’s name to the list.

Code examples

The following example updates the participant list on the UI with the names of all active room participants. To call this method, you pass Room.Participants in the argument.

        /// <summary>
        /// Fills a list box with the display names of active room participants.
        /// </summary>
        /// <param name="users"></param>
        private void UpdateParticipantList(IList<RoomUser> users)
        {
            RoomRoster_Listbox.Items.Clear();

            //Iterate on list of room users
            foreach (RoomUser roomUser in users)
            {
                //Get the display name of a room user.
                if (roomUser.Uri != null)
                {
                    string displayName = _client.ContactManager.GetContactByUri(roomUser.Uri).GetContactInformation(ContactInformationType.DisplayName).ToString();

                    //Add the display name to the list box if it is not already in the list box.
                    if (!RoomRoster_Listbox.Items.Contains(displayName))
                    {
                        RoomRoster_Listbox.Items.Add(displayName);
                    }
                }
            }
        }

Keep the UI room participant list current by handling the events in this section.

Code examples

The following examples handle the Room.ParticipantAdded and Room.ParticipantRemoved events.

        /// <summary>
        /// Handles the Room.ParticipantAdded event. The event is raised when a room member starts participating in a room.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void room_ParticipantAdded(object sender, RoomParticipantsEventArgs e)
        {
            try
            {
                //Invoke a method on the UI thread that updates the room participant list.
                RoomRoster_Listbox.Invoke(new FillRoomRosterCallback(this.FillRoomRoster), (Room)sender);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception in Room.ParticpantAdded : " + ex.Message);
            }

        }

        /// <summary>
        /// Handles the Room.ParticipantRemoved event. The event is raised when a room member stops participating in a room
        /// by signing out of Lync
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void room_ParticipantRemoved(object sender, RoomParticipantsEventArgs e)
        {
            try
            {
                //Invoke a method on the UI thread that updates the room participant list.
                RoomRoster_Listbox.Invoke(new FillRoomRosterCallback(this.FillRoomRoster), (Room)sender);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception in Room.ParticpantRemoved : " + ex.Message);
            }

        }


The following example is invoked on the UI thread by the platform thread when a participant collection event is raised.

        //Delegate that is invoked by the Room Manager followed room collection events
        private delegate void FillRoomRosterCallback(Room room);

        /// <summary>
        /// Invoked by platform thread when a participant is added or removed from a room
        /// </summary>
        /// <param name="room"></param>
        private void FillRoomRoster(Room room)
        {
            //Participants property count is zero if a user is not in the room.
            //If a user is not in a room, the first message that the user sends to a room 
            //causes the user to enter the room.
            if (room.Participants.Count > 0)
            {
                UpdateParticipantList(room.Participants);
            }
        }



Community Additions

Show:
© 2014 Microsoft