Walkthrough: Sign In to Lync (Lync 2010 SDK)

This topic shows how to sign a client application in to Microsoft Lync 2010 where the Lync user interface is not suppressed.

Prerequisites

Important

Before you start the walkthrough, create an asynchronous callback method that is invoked when the sign-in operation is is completed. You may decide to pass a null value instead of a callback method. If you want to be notified that the user completed the sign-in process, you should create an event handler for the StateChanged event that is raised when the state of a client has changed.

Signing In to Lync

The classes, methods, and events used in the Lync 2010 sign-in process appear in the following illustration.

To Sign In to Lync

  1. Create callback methods to handle sign-in related events.

  2. Get the LyncClient instance. The Microsoft.Lync.Model namespace exposes the LyncClient class. You call the static GetClient method on that class to obtain an instance of LyncClient.

  3. Get the UI Suppression state of the client by reading the InSuppressedMode property.

    If the return value is true, the client may have to be initialized.

  4. Register for the StateChanged event for this instance of LyncClient.

  5. Get the current client state.

    If the current state of the client is ClientState.SignedIn, you should compare the value of the Contact.Uri property on the Contact you get from Self.Contact with the Uri provided by a user signing in to Lync from your application. If the values are not equal, the user to be signed in is not the currently signed in user.

  6. Call BeginSignOut to sign the current user out if the new user is not the current user.

    Tip

    After calling BeginSignOut, you should call BeginSignIn for the new user. Code the BeginSignIn call inside an asynchronous callback.

  7. Call the BeginSignIn method. The first three parameters should not be passed as null. The user URI, name, and password are obtained from the operating system when the UI Suppression mode is disabled on Lync. When UI Suppression is enabled, these parameters must not be null. You can pass a callback delegate and a state object. If you do not pass a callback delegate to BeginSignIn, you must pass a null value.

Handling Client State Changes

The StateChanged event is raised when the state of the client changes. You can use this event to trigger an update to your UI with the current sign-in status of the client. This event handler can be used to obtain the classes that LyncClient exposes. Before you get classes from the client, you must wait until your application receives a new ClientState.SignedIn state.

To handle StateChanged events, create the method designated as the event handler when you registered for the StateChanged event in the previous procedure.

Examples

The following examples sign a user into Lync 2010 and handle related events.

Get the Client

The following example obtains an instance of LyncClient.

        #region private fields
        private LyncClient _LyncClient;

        #endregion
        /// <summary>
        /// Custom application class constructor. This class wraps the functionality of LyncClient
        /// </summary>
        public ClientModel()
        {
            try
            {
                _LyncClient = LyncClient.GetClient();

                if (_LyncClient == null)
                {
                    throw new Exception("Unable to obtain client interface");
                }

                if (_LyncClient.InSuppressedMode == true)
                {
                    if (_LyncClient.State == ClientState.Uninitialized)
                    {
                        Object[] _asyncState = { _LyncClient };
                        _LyncClient.BeginInitialize(InitializeCallback, _asyncState);
                    }
                }
                _LyncClient.StateChanged += _Client_ClientStateChanged;
            }
          catch (NotStartedByUserException)
          {
              throw new Exception("Lync is not running");
          }
        }

        /// <summary>
        /// Handles the asynchronous initialize callback invoked by a client instance upon initialize
        /// </summary>
        /// <param name="initializedClient">LyncClient. The initialized client.</param>
        /// <param name="AsyncOp">IAsyncResult. The async interface exposing the results of the operation.</param>
        private void InitializeCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted == true)
            {
                object[] asyncState = (object[])ar.AsyncState;
                ((LyncClient)asyncState[0]).EndInitialize(ar);
            }
        }

Check Client State and Sign In

The following example signs the local user in to Lync Server 2010. The example instantiates a local delegate to pass as the callback method parameter.

        /// <summary>
        /// Method to sign in to a LyncClient instance
        /// </summary>
        /// <param name="UserUri">string. Uri of user.</param>
        /// <param name="Domain">string. Domain of user.</param>
        /// <param name="Password">string. Password of user</param>
        public void SignIn(string UserUri, string Domain, string Password)
        {

            //this method returns an interface that can be used for further work
            if (_LyncClient == null)
            {
                return;
            }

            if (_LyncClient.State != ClientState.SignedIn)
            {
                if (_LyncClient.State == ClientState.Invalid)
                {
                    throw new Exception("Lync is in an invalid state");
                }
                if (_LyncClient.State != ClientState.SignedIn)
                {
                    try
                    {

                        object[] _asyncState = { _LyncClient, "" };
                        _LyncClient.BeginSignIn(
                             UserUri,
                            Domain,
                            Password,
                            SigninCallback,
                            _asyncState);
                    }
                    catch (NotInitializedException)
                    {
                        throw new Exception("Lync is not initialized");
                    }
                }
            }
        }

Handle Client State Changed Event

The following example handles the StateChanged event.

        /// <summary>
        /// Handles UCClient state change events. 
        /// </summary>
        /// <param name="source">Client.  Instance of UCClient as source of events.</param>
        /// <param name="data">ClientStateChangedEventArgs. State change data.</param>
        void _LyncClient_ClientStateChanged(Object source, ClientStateChangedEventArgs data)

        {
            if (data.NewState == ClientState.SignedIn)
            {
                MessageBox.Show("The user is signed in. Proceed to communicate.");
            }
        }

Client Operation Asynchronous Callback

The following example is invoked when the BeginSignIn operation is complete.

        /// <summary>
        /// callback method called by LyncClient.SignIn()
        /// </summary>
        /// <param name="source">LyncClient</param>
        /// <param name="_asyncOperation">IAsynchronousOperation callback</param>
        /// 
        public void SigninCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted == true)
            {
                object[] asyncState = (object[])ar.AsyncState;
                ((LyncClient)asyncState[0]).EndSignIn(ar);
            }
            
        }

Next Steps

Your client is now in a state where you can execute all Lync 2010 API functionality. You can obtain the users contacts, start and accept conversations, and complete the user sign-out process.

See Also

Tasks

Walkthrough: Sign In to Lync with UI Suppressed (Lync 2010 SDK)

Concepts

Lync Model API Concepts (Lync 2010 SDK)