Note: this post is part of Lync Client development series. You can find the following topics here:
- Microsoft Lync Client Development,
- Sign in and out in Microsoft Lync Client,
- Lync Controls inside Lync Client SDK,
- Starting with Lync Client’s API,
- Handling some common scenarios with Lync Conversation,
- Handling transfers in Lync Client API,
- Handling conference calls in Lync Client,
- Availability (presence) in Lync Client.
Hello everyone,
First things – first: I will present you shortly how to perform programmatic sign-in and out by using Microsoft Lync Client API, and what obstacles and options you have.
For all this stuff, the main object to use is of course LyncClient class, which is a class inside Microsoft.Lync.Model namespace, resposible for all things you do about Lync. I usually start with the GetClient method to retrieve the instance of this class locally and store it in some wrapper class (and make it IDisposable). With that, you are certain that the Lync Client is installed and running on a local computer:
try
{
// for the call to the GetClient method to succeed, Lync 2010 must be running on the local computer
m_lyncClient = LyncClient.GetClient();
SubscribeToLyncEvents();
}
catch (ClientNotFoundException e)
{
throw new CommunicationException(Resources.Lync_Error_NotInstalledOrNotRunning, e);
}
Notice that the special exception is catched - you will encounter them on the way, and learn how to react on it.
After this is settled and we have an instance of the LyncClient class, we have to check whether the UI Suppression Mode is activated, and if it is, the application should initialize the Lync Client. This is important, because the connection to Lync won’t work if the initialization is not done if Lync has been suppressed:
if (m_lyncClient.InSuppressedMode)
{
throw new CommunicationException(“Microsoft Lync Client is in UI Suppression Mode.”);
if (m_lyncClient.State == ClientState.Uninitialized)
{
// initializes LyncClient when the Lync is in Suppressed Mode
// this method must not be called if the Lync is not suppressed
object[] asyncState = { m_lyncClient };
IAsyncResult asyncResult = m_lyncClient.BeginInitialize(InitializeCallback, asyncState);
// wait for the results because there is no point to continue any further before this work is done.
asyncResult.AsyncWaitHandle.WaitOne();
}
Notice that thread will wait on initialization to finish, because there are two many different scenarios to support if this is just left async. Nextis the initialization callback defined:
private void InitializeCallback(IAsyncResult result)
{
if (result.IsCompleted)
{
object[] asyncState = (object[])result.AsyncState;
((LyncClient)asyncState[0]).EndInitialize(result);
}
}
Now, the sign-in process. On the forum, there are couple of different scenarios and solutions to it, but since I’m using the Lync not to change currently logged-in user and instead just reusing his credentials, signing in in fairly easy:
// if the local user is already signed in to Lync Server 2010, calling this method raises an exception
if (m_lyncClient.State == ClientState.SignedIn)
{
SubscribeToSelfEvents();
}
else
{
if (m_lyncClient.State == ClientState.Invalid)
{
throw new CommunicationException(Resources.Lync_Error_InvalidState);
}
try
{
object[] asyncState = { m_lyncClient, “” };
// store the credentials for later use, only if the sign in process is required
m_strUserName = strUserName;
m_strPassword = strPassword;
// starts the sign in process asynchronously
IAsyncResult asyncResult = m_lyncClient.BeginSignIn(strSipNumber, strUserName, strPassword, SignInCallback, asyncState);
// But wait for the results because the events cannot be registered within a worker thread.
asyncResult.AsyncWaitHandle.WaitOne();
SubscribeToSelfEvents();
}
catch (NotInitializedException e)
{
throw new CommunicationException(Resources.Lync_Error_SignInNotPossible, e);
}
}
And here is the corresponding sign-in callback:
private void SignInCallback(IAsyncResult result)
{
if (result.IsCompleted == true)
{
object[] asyncState = (object[])result.AsyncState;
((LyncClient)asyncState[0]).EndSignIn(result);
}
}
You have to notice here some of the key points: SubscribeToSelfEvents is the method to create event handlers for Self object (representing a Contact which is currently signed in). This method is called only when it is confirmed that we are successfully signed in into Lync, otherwise we would get errors in event handlers for Self object. You have to take into account that this piece of code will be usually located inside some wrapper class, and therefore probably running on a separate thread (worker thread). So, you should pay attention not to raise any GUI related or async code that might result in mixing across threads.
Sign-out process is also very simple – you just perform the same style to sign the user out of Lync by using BeginSignOut method and implement the corresponding callback:
// if the local user is already signed out from the Lync Server 2010, calling this method raises an exception
if (m_lyncClient.State == ClientState.SignedIn)
{
if (m_lyncClient.State == ClientState.Invalid)
{
throw new CommunicationException(Resources.Lync_Error_InvalidState);
}
try
{
object[] asyncState = { m_lyncClient, “” };
// starts the sign out process asynchronously
IAsyncResult asyncResult = m_lyncClient.BeginSignOut(SignOutCallback, asyncState);
// Wait for the results because the events should be unregistered within main thread.
// Otherwise it can be trouble if this instance is not fully released when signing in again but still in running application.
asyncResult.AsyncWaitHandle.WaitOne();
}
catch (NotInitializedException e)
{
throw new CommunicationException(Resources.Lync_Error_SignOutNotPossible, e);
}
}
private void SignOutCallback(IAsyncResult result)
{
if (result.IsCompleted == true)
{
object[] asyncState = (object[])result.AsyncState;
((LyncClient)asyncState[0]).EndSignOut(result);
}
}
That’s it for now. In the next post, I will present you the options you have in using Lync Controls for .NET that come out-of-the-box from Lync SDK.
Hi, my name is Guido from belgium and I could need your assistance. I need to create a small C# or .net application based on the Lync api, but I’m not used to program in these languages. Is it possible to get in contact to discuss?
Best regards,
Guido
By: Guido on 25/11/2011
at 15:04