This topic demonstrates how to sign a client application in to Microsoft Lync 2010.

Prerequisites

  • Create a new Microsoft Visual C# Windows Forms Application. For more information, see Walkthrough: Start a New Application .

Important note Important

Before you start the walkthrough, create an asynchronous callback method that is invoked when the sign-in operation is complete. You may choose to pass a null value in place 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.

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 static LyncClient class. You call the GetClient method on that class to obtain an instance of LyncClient. For more information, see Walkthrough: Start a New Application .

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

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

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

  5. Get the current client state.

    If the client state is ClientState.Uninitializedand the client is in full UI Suppression mode, you must initialize the client by calling into BeginInitialize .

    If the current state of the client is ClientState.SignedIn, you should compare the value of the Contact . . :: . . Uri property on the Contactyou 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.

    If the current state of the client is not ClientState.SignedIn, proceed to step 8.

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

    Tip Tip

    After calling BeginSignOut , you should call BeginSignIn for the new user. Code the BeginSignIncall within 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 StateChangedevent is raised whenever 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 LyncClientexposes. Before you get classes from the client, you must wait until your application receives a new ClientState.SignedInstate.

To handle StateChangedevents, create the method designated as the event handler when you registered for the StateChangedevent 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.

C#  Copy imageCopy Code
		#region private fields
		private LyncClient _LyncClient;

		#endregion


		/// <summary>
		/// Custom application class constructor. This class wraps
the functionality of UCClient
		/// </summary>
		public ClientModel()
		{


			try
			{
				_LyncClient = LyncClient.GetClient();


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

				if (_LyncClient.InFullUISuppressionMode == true)
				{
					if (_LyncClient.State ==
ClientState.Uninitialized)
					{
						Object[] _asyncState = { _LyncClient };
					 
_LyncClient.BeginInitialize(InitializeCallback, _asyncState);
				}
			}
				_LyncClient.StateChanged +=
_Client_ClientStateChanged;

		}

			catch (NotStartedByUserException h)
			{
				throw new ClientWrapException(h, "Lync is not
running", true);
		}
			catch (Exception ex)
			{
				throw new ClientWrapException(ex, "General
Exception on static GetClient() call", true);
		}
	}

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.

  Copy imageCopy Code
		/// <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 ClientWrapException(null, "Lync is in
an invalid state", true);
			}
				if (_LyncClient.State != ClientState.SignedIn)
				{
					try
					{

						object[] _asyncState = { _LyncClient, "" };
						_LyncClient.BeginSignIn(
							 UserUri,
							Domain,
							Password,
							SigninCallback,
							_asyncState);


				}
					catch (NotInitializedException nse)
					{
						throw new ClientWrapException(nse, "Lync is
not initialized", true);

				}
			}
		}
	}


Handle Client State Changed Event

The following example handles the StateChangedevent.

C#  Copy imageCopy Code
		/// <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 BeginSignInoperation is complete.

  Copy imageCopy Code
		/// <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

Concepts

Other Resources

Walkthrough: Start a New Application