Share via


Application Session Class: Code Listing

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example class handles the invitation of remote users to a conference initiated by a local user. The class does not implement the collaboration functionality necessary to participate in a conference after a remote user accepts an invitation and joins a meeting. For more information, see Joining and Leaving Conferences.

Code Listing

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.UccApi;
using System.Xml;
using UccpAppComponents;



namespace UccpAppComponents
{
    public class UCCPApplicationSession :
        _IUccApplicationSessionParticipantEvents,
        IDisposable
    {

        #region public delegates
        public delegate void ConfInviteReceived(
            IUccSession ConferenceSession,
            string ConferenceURI,
            string ConferenceSubject,
            string IMGreeting,
            Boolean IMAvailable);

        #endregion


        #region private fields
        string myConferenceUri = string.Empty;
        string myConferenceSubject = string.Empty;
        string myFirstIMGreeting = string.Empty;
        Boolean myIMAvailable = false;
        UccUriManager myUriManager = new UccUriManagerClass();
        UccUri myRemoteUser = null;
        string myXMLConfData = string.Empty;
        IUccSession myApplicationSession;
        private Boolean disposed = false;
        IUccSessionParticipant myRemoteParticipant;
        IUccApplicationSessionParticipant myAppParticipant;
        #endregion

        #region public properties
        public string ConferenceUri
        {
            set
            {
                this.myConferenceUri = value;
            }
            get
            {
                return this.myConferenceUri;
            }
        }

        public string ConferenceSubjecet
        {
            set
            {
                this.myConferenceSubject = value;
            }
            get
            {
                return this.myConferenceSubject;
            }
        }

        public Boolean IMAvailable
        {
            set
            {
                this.myIMAvailable = value;
            }
            get
            {
                return this.myIMAvailable;
            }
        }

        public string IMGreeting
        {
            set
            {
                this.myFirstIMGreeting = value;
            }
            get
            {
                return this.myFirstIMGreeting;
            }
        }

        public string XMLConfData
        {
            set
            {
                this.myXMLConfData = value;
            }

            get
            {
                return this.myXMLConfData;
            }
        }
        #endregion

        #region public methods

        /// <summary>
        /// Sends a conference invitation to the specified remote
        /// participant
        /// </summary>
        /// <param name="pRemoteUri">string remote user Uri</param>
        public void ConferenceInvite(UccUri pRemoteUri)
        {

            // Verify calling code has set a remote participant URI string. Throw exception
            // up to calling code if not set.
            if (pRemoteUri == null)
            {
                throw new ArgumentNullException(
                    "pRemoteUri", 
                    "Remote user Uri must be set");
            }

            // If conference URI property is not set for this application session,
            // throw exception to calling code.
            if (this.ConferenceUri == string.Empty)
            {
                throw new Exception("Conference Uri Property is not set");
            }

            // If IM is available for this conference and an initial greeting property has
            // not been set, then default an initial IM greeting.
            if (this.IMAvailable == true && this.IMGreeting == string.Empty)
            {
                this.myFirstIMGreeting = "Conference Invitation";
            }


            // Create context for creation of remote user and addition of remote user to session.
            UccContext inviteContext = new UccContextClass();
            inviteContext.AddNamedProperty(
                "RemoteUser",
                pRemoteUri.AddressOfRecord);

            // Create new session participant.
            this.myRemoteParticipant =  this.myApplicationSession.CreateParticipant(
                pRemoteUri, 
                inviteContext);

            // Cast session participant to application session participant.
            this.myAppParticipant = this.myRemoteParticipant as IUccApplicationSessionParticipant;

            
            // Advise for application session participant events and set event handler to this class.
            UCC_Advise<_IUccApplicationSessionParticipantEvents>(
                this.myAppParticipant, 
                this);

            // Create operation context class and set existing context as operation context.
            UccOperationContext inviteOperationContext = new UccOperationContextClass();
            inviteOperationContext.Initialize(1,inviteContext);

            // Add the participant to the session. 
            // This operation triggers the application invitation to be sent and raises
            // OnOutgoingInvitation.
            this.myApplicationSession.AddParticipant(
                this.myRemoteParticipant,
                inviteOperationContext);
        }

        #endregion


        #region public events
        public event ConfInviteReceived ConferenceInvitation;
        #endregion

        #region event handlers

        /// <summary>
        /// Remote user has accepted invitation
        /// </summary>
        /// <param name="pEventSource"></param>
        /// <param name="pEventData"></param>
        void _IUccApplicationSessionParticipantEvents.OnInvitationAccepted(
            UccApplicationSessionParticipant pEventSource,
            UccInvitationAcceptedEvent pEventData)
        {
            // Dispose of this application session. 
            this.Dispose();
        }


        /// <summary>
        /// Before outgoing invitation is sent, callback function injects
        /// XML string containing the Conference URI, IM availability, 
        /// and the first IM greeting
        /// </summary>
        /// <param name="pEventSource"></param>
        /// <param name="pEventData"></param>
        void _IUccApplicationSessionParticipantEvents.OnOutgoingInvitation(
            UccApplicationSessionParticipant pEventSource,
            UccOutgoingInvitationEvent pEventData)
        {

            
            // Initialize XML string.
            string sessionDesc = string.Empty;

            // If XML string property is set, use in place of default XML string.
            if (this.myXMLConfData.Length > 0)
            {
                sessionDesc = this.myXMLConfData;
            }

            // Otherwise, build default string using previously set property values.
            else
            {
                    string boolString = string.Empty;
                    if (this.myIMAvailable == true)
                    {
                        boolString = System.Boolean.TrueString;
                    }
                    else
                    {
                        boolString = System.Boolean.FalseString;
                    }


                sessionDesc = @"<Conferencing version=""2.0"">"
                + "<focus-uri>"
                + this.myConferenceUri
                + "</focus-uri><subject>"
                + this.myConferenceSubject
                + "</subject>"
                + "<im available=\""
                + boolString
                + "\"><first-im>"
                + this.myFirstIMGreeting
                + "</first-im></im></Conferencing>";
            }

            pEventData.ContentType = "application/ms-conf-invite+xml";
            pEventData.SessionDescription = sessionDesc;
            pEventData.Send();

        }
        void _IUccApplicationSessionParticipantEvents.OnRenegotiate(
            UccApplicationSessionParticipant pEventSource,
            IUccOperationProgressEvent pEventData)
        {
            MessageBox.Show("OnInvitationExcepted");
        
        }

        #endregion

        #region constructor/destructor


        /// <summary>
        /// Creates an instance of application session. This constructor is used to initiate outgoing 
        /// conference invitations
        /// </summary>
        /// <param name="pApplicationSession">UCCPSession instance of application session wrapper class</param>
        public UCCPApplicationSesion(IUccSession pApplicationSession)
        {
            if (pApplicationSession == null)
            {
                throw new ArgumentNullException("pApplicationSession", "UCCPSession parameter cannot be null");
            }

            // Set private UCCPSession field.
            this.myApplicationSession = pApplicationSession;

        }

        /// <summary>
        /// Creates an instance of an application session for an incoming application session invitation of 
        /// type IUccSession
        /// </summary>
        /// <param name="pApplicationSession">IUccSession application session</param>
        /// <param name="pParticipant">IUccSessionParticipant remote conference participant</param>
        /// <param name="pConferenceUri">string conference URI to join</param>
        /// <param name="pSessionEventData">UccIncomingSessionEvent OnIncomingSession pEventData </param>
        public UCCPApplicationSesion(
            IUccSession pApplicationSession, 
            IUccSessionParticipant pParticipant,
            string pConferenceUri,
            UccIncomingSessionEvent pSessionEventData)
        {
            if (pApplicationSession == null)
            {
                throw new ArgumentNullException("pApplicationSession", "UCCPSession parameter cannot be null");
            }

            // Set private UCCPSession field.
            this.myApplicationSession = pApplicationSession;

            // Cast remote session participant to application session participant and store in
            // class private field.
            this.myAppParticipant = pParticipant as IUccApplicationSessionParticipant;

            // Advise for application session participant events and set event handler to this class.
            UCC_Advise<_IUccApplicationSessionParticipantEvents>(
                this.myAppParticipant,
                this);

            // Accept the incoming application session invitation.
            pSessionEventData.Accept();
            
        }

        ~UCCPApplicationSesion()
        { }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(Boolean disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.myRemoteParticipant != null)
                    {
                        UCC_Unadvise<_IUccApplicationSessionParticipantEvents>(this.myRemoteParticipant, this);
                    }
                }
                this.disposed = true;
            }
        }

        #endregion


    }
}

See Also

Concepts

Application Session Class: Code Listing
Conference Sessions