How to: Sign in to and out of Lync

How to

Learn how to sign in a client application to Microsoft Lync 2013, sign out a user of Lync 2013, and shut the Lync 2013 process down with UI suppression enabled.

_Applies to:  Lync 2013 | Lync Server 2013 _

In this article
Process overview
Sign in to Lync, UI not suppressed
Sign in to Lync, UI suppressed
Sign out of Lync and shut down the Lync process when in UI suppression mode
Code example: Sign in to Lync Server and shut down Lync Server
Additional resources

Watch the video: Sign In to Lync with UI Suppressed

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/5d247b33-af17-45f6-ad7e-9be9f9f7e5cd]
Watch the video: Sign In to Lync

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/8a417ed0-bcab-4da7-b033-d1e2cdb784df]

Process overview

Signing a user in to Microsoft Lync 2013 can be as simple as making a single method call. It can also be a more complex operation that includes four method calls, two event callback methods, and a property value. In the first case, the simple sign-in scenario is run when the Lync 2013 client is not in UI suppression mode and the same user who signed in to Windows is also signing in to Lync. The most complex scenario is where Lync 2013 is running in UI suppression mode. In this case, you must perform a set of operations in a specific order and handle operational events between operations.

The following illustration shows the flow of application logic that can handle any sign-in scenario.

OCOM_SignIn_NotInitialized

Sign in to Lync, UI not suppressed

To sign in the user who signed in to Windows

  1. Register an event callback on the Client.StateChanged event.

  2. Get the value of the Client.State property.

  3. Compare the value of the property from step 1 to the ClientState.SignedOut enumerator.

  4. If the property value equals ClientState.SignedOut, call the LyncClient.BeginSignIn method.

    The following example signs a user in to Lync 2013 with the network credentials used to sign in to Windows.

                if (_LyncClient.State == ClientState.SignedOut)
                {
                    _LyncClient.BeginSignIn(
                        null,
                        null,
                        null,
                        (ar) =>
                        { _LyncClient.EndSignIn(ar);},
                        null);
                }
    
  5. Handle the Client.StateChanged event.

    When the state of the client changes to ClientState.SignedI, the user is signed in.

To sign in a different user

  1. Register an event callback on the Client.StateChanged event.

  2. Get the value of the Client.State property.

  3. Compare the value of the property from step 1 to the ClientState.SignedOut enumerator.

  4. If the property value equals ClientState.SignedOut, call the LyncClient.BeginSignIn method.

    The following example signs a user in to Lync 2013 with the credentials that the user supplied in an application credential challenge dialog box.

                if (_LyncClient.State == ClientState.SignedOut)
                {
                    _LyncClient.BeginSignIn(
                        ″andrewr@contoso.com″,
                        ″andrewr@contoso.com″,
                        ″ds3ew_exws!!″,
                        (ar) =>
                        { _LyncClient.EndSignIn(ar);},
                        null);
                }
    

    Note

    If any of the three elements of these credentials are not valid, the LyncClient.CredentialRequested event is raised. After prompting the user to enter valid credentials, resubmit the credentials by calling the CredentialRequestedEventArgs.Submit method.

  5. Handle the Client.StateChanged event.

    When the state of the client changes to ClientState.SignedIn, the user is signed in.

Sign in to Lync, UI suppressed

Signing in to Lync 2013 in UI suppression mode involves one additional step. You must check to see whether the Lync client is initialized. If it is not initialized, initialize it before you sign in.

To initialize and sign in the Lync 2013 client

  1. Read the value of the Client.State property.

    Go to step 3 if the property value is ClientState.SignedOut.

  2. Call the LyncClient.BeginInitialize method.

    When the client platform is initialized, the new state of the client is ClientState.SignedOut.

            if (_LyncClient.State == ClientState.Uninitialized)
            {
                _LyncClient.BeginInitialize((ar) =>
                    {
                        //Set a class field flag so that the application 
                        //shuts down the Lync platform when the application closes
                        _ThisInitializedLync = true;
                        _LyncClient.EndInitialize(ar);
                        _LyncClient.StateChanged += _LyncClient_ClientStateChanged;
                    },
                    null);
            }
    
  3. In the event callback for the Client.StateChanged event, verify that the state of the client is SignedOut, and then sign in the client.

            /// <summary>
            /// Handles LyncClient state change events.
            /// </summary>
            /// <param name="source">Client.  Instance of LyncClient as source of events.</param>
            /// <param name="data">ClientStateChangedEventArgs. State change data.</param>
            void _LyncClient_ClientStateChanged(Object source, ClientStateChangedEventArgs data)
            {
                  if (data.NewState == ClientState.SignedOut)
                {
                    if (_LyncClient.InSuppressedMode == true)
                    {
                        //Helper method, see full example at end of topic.
                        SignIn(null, null, null);
                    }
                }
            }
    

    Tip

    The previous example checks to see whether the client is suppressed before calling the SignIn helper method. When in UI suppression mode, initializing the client changes the state from Uninitialized to SignedOut. In this mode, you want to transition from SignedOut to SignedIn. In non-UI-suppression mode, the normal transition is from SignedIn to SignedOut.

Sign out of Lync and shut down the Lync process when in UI suppression mode

If the Lync client is in UI suppression mode, it is important that your application shuts down the Lync 2013 platform when the application closes. There are two critical conditions that must be true before you shut down the platform. These conditions are as follows:

  • The Lync 2013 platform must be in UI suppression mode. Read the value of the LyncClient.InSuppressedMode property. If the value is true, the client UI is suppressed.

  • This instance of your application must have initialized the platform. When this application initializes the platform, set a class field flag that indicates this application initialized the platform. Read the flag when this application is ready to shut down the platform.

To sign out and shut down

  1. Unregister the Client.StateChanged event handler.

    Your application can respond to any state changes in the callback method that you pass into the methods called in this process. The event hander is no longer needed.

  2. Check the state of the client by reading the Client.State property.

    If the client is signed in, sign out before shutting down the client.

                    _LyncClient.BeginSignOut((ar) =>
                        {
                            _LyncClient.EndSignOut(ar);
                            //... 
                        },
                    null);
    
  3. If the client is signed out, shut down the client if the two previous conditions are met.

    //Lync must be shut down if in UI suppresion mode, closing this window, and
    //this application initialized Lync.
    if (_LyncClient.InSuppressedMode == true && _ThisInitializedLync == true)
    {
        //Helper method... see full example below.
        ShutClientDown();
    }
    
        /// <summary>
        /// Unregisters for client state change event, signs out, and shuts down Lync if in
        /// UI suppression mode. Closes window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            //turn off event registration for state change
            _LyncClient.StateChanged -= _LyncClient_ClientStateChanged;

            if (_LyncClient.State == ClientState.SignedIn)
            {
                _LyncClient.BeginSignOut((ar) =>
                {
                    _LyncClient.EndSignOut(ar);

                    //Lync must be shut down if in UI suppresion mode, closing this window, and
                    //this application initialized Lync.
                    if (_LyncClient.InSuppressedMode == true && _ThisInitializedLync == true)
                    {
                        ShutClientDown();
                    }
                    else
                    {
                        //Close the main window
                        this.Dispatcher.Invoke(new delegateMethod(this.Close));
                    }
                },
                null);
            }
            else if (_LyncClient.State == ClientState.SignedOut)
            {
                //Lync must be shut down if in UI suppresion mode, closing this window, and
                //this application initialized Lync.
                if (_LyncClient.InSuppressedMode == true && _ThisInitializedLync == true)
                {
                    ShutClientDown();
                }
                else
                {
                    //Close the main window
                    this.Close();
                }
            }

        }

        private void ShutClientDown()
        {
            _LyncClient.BeginShutdown(
                (ar2) =>
                {
                    _LyncClient.EndShutdown(ar2);

                    //Close the main window
                    this.Dispatcher.Invoke(new delegateMethod(this.Close));
                }
                , null);
        }
        delegate void delegateMethod();

Code example: Sign in to Lync Server and shut down Lync Server

The following example shows signing in to Lync Server and shutting down Lync Server.

using System.Windows;
using Microsoft.Lync.Model;
using System.Collections.Generic;
using System;
namespace CustomAvailabilityStates
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        LyncClient _LyncClient;
        Boolean _ThisInitializedLync = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _LyncClient = LyncClient.GetClient();

                    if (_LyncClient.State == ClientState.Uninitialized)
                    {
                        _LyncClient.BeginInitialize((ar) =>
                            {
                                _ThisInitializedLync = true;
                                _LyncClient.EndInitialize(ar);
                                _LyncClient.StateChanged += _LyncClient_ClientStateChanged;

                            },
                            null);
                    }
                    else
                    {
                        _LyncClient.StateChanged += _LyncClient_ClientStateChanged;
                        SignIn(″andrewr@contoso.com″, ″andrewr@contoso.com″, ″x89_4gWrd″);

                    }

            }
            catch (ClientNotFoundException) 
            {
                MessageBox.Show("The Lync client is not running");
            }
            catch (NotStartedByUserException)
            {
                MessageBox.Show("Lync is not running");
            }
        }

        /// <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.SignedOut)
            {
                if (_LyncClient.InSuppressedMode == true)
                {
                    SignIn(″andrewr@contoso.com″, ″andrewr@contoso.com″, ″x89_4gWrd″);
                }
            }
        }

        /// <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)
        {
              _LyncClient.CredentialRequested += _LyncClient_CredentialRequested;
            if (_LyncClient.State == ClientState.SignedOut)
            {
                _LyncClient.BeginSignIn(
                     UserUri,
                    Domain,
                    Password,
                    (ar) =>
                    {
                        _LyncClient.EndSignIn(ar);
                    },
                    null);
            }
        }
        /// <summary>
        /// Unregisters for client state change event, signs out, and shuts down Lync if in
        /// UI suppression mode. Closes window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            //turn off event registration for state change
            _LyncClient.StateChanged -= _LyncClient_ClientStateChanged;

            if (_LyncClient.State == ClientState.SignedIn)
            {
                _LyncClient.BeginSignOut((ar) =>
                {
                    _LyncClient.EndSignOut(ar);

                    //Lync must be shut down if in UI suppresion mode, closing this window, and
                    //this application initialized Lync.
                    if (_LyncClient.InSuppressedMode == true && _ThisInitializedLync == true)
                    {
                        ShutClientDown();
                    }
                    else
                    {
                        //Close the main window
                        this.Dispatcher.Invoke(new delegateMethod(this.Close));
                    }
                },
                null);
            }
            else if (_LyncClient.State == ClientState.SignedOut)
            {
                //Lync must be shut down if in UI suppresion mode, closing this window, and
                //this application initialized Lync.
                if (_LyncClient.InSuppressedMode == true && _ThisInitializedLync == true)
                {
                    ShutClientDown();
                }
                else
                {
                    //Close the main window
                    this.Close();
                }
            }

        }

        private void ShutClientDown()
        {
            _LyncClient.BeginShutdown(
                (ar2) =>
                {
                    _LyncClient.EndShutdown(ar2);

                    //Close the main window
                    this.Dispatcher.Invoke(new delegateMethod(this.Close));
                }
                , null);
        }
        delegate void delegateMethod();

        /// <summary>
        /// Raised when credentials are required by a server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _LyncClient_CredentialRequested(object sender, CredentialRequestedEventArgs e)
        {
            //If the server type is Lync server and sign in credentials
            //are needed.
            if (e.Type == CredentialRequestedType.SignIn)
            {
                //Re-submit sign in credentials
                e.Submit("andrewr@contoso.com", "d8wd_eedd",true);
            }
        }
    }
}

Additional resources