Cookie CSS

Saturday, January 28, 2017

Building a Modern Skype for Business Profile Switcher - Part II

Last week I started building a simple Skype for Business profile switcher with a modern looking UI.  After last weeks post, I finished the product and uploaded it to technet.  You can download it using this link.

Download S4BSwitcher


This week we'll dive into the source code that does the heavy lifting, logging out the current user, and logging in the user we selected.

If you remember from last week we had defined a variable that allowed us to connect to the existing running Skype for Business client.

Globals.lc = LyncClient.GetClient();

So using that we will call a function that looks like this.

        private void switchSignin(string user, string pw, string signin)
        {
            try
            {
                if (Globals.lc.State == ClientState.SignedIn)
                {
                    Globals.lc.BeginSignOut(myar => 
                    {
                        try
                        {
                            Globals.lc.EndSignOut(myar);
                        }
                        catch (Exception ex)
                        {
                            this.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                MessageBox.Show(ex.Message);
                            }));
                        }
                    }, null);

                    while (Globals.lc.State != ClientState.SignedOut)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }

                System.Threading.Thread.Sleep(1000);

                Globals.lc.BeginSignIn(signin, user, pw, myar => 
                {
                    try
                    {
                        Globals.lc.EndSignIn(myar);
                    }
                    catch (Exception ex)
                    {
                        this.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            MessageBox.Show(ex.Message);
                        }));
                    }
                }, null);
            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    MessageBox.Show(ex.Message);
                }));
            }

                           
        }

You can see that our function takes 3 parameters.

1.  The sign in - joe.user@domain.com
2.  The password
3.  The user name domain\joe.user

So once we have this info, we begin a simple process.  First we check the client status to see if it is signed in, if so we begin the process to sign out the current user.  If the user is not signed in there is no point of this step, so hence the check.

Next we wait until the client indicates it has reached the signed out state.  Once we are there, we begin the sign in phase.  On the client object we call the BeginSignIn function with our 3 parameters with a callback of EndSignIn.  If any errors occur along the we with present them to the user with the messagebox function.  You will note all of our messagebox calls are wrapped inside a dispatcher begininvoke statement.  This allows us to run our function in a non GUI thread allowing for a nice smooth program operation, at which time we display a wait spinner as shown above.


There you have it, it should now be relatively easy for you to manipulate your Skype for Business client using the Microsoft Lync Client SDK.

Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge