This topic demonstrates how to sign a client application in to Microsoft Lync 2010 when the application UI is suppressed. While in suppression mode, Lync 2010 must be initialized by your custom process.

Lync 2010 operating in the UI Suppression mode is designed to support a single Lync 2010 API enabled process at a time. You should examine the Lync 2010 state before initializing it. If the state is already initialized and your custom process did not initialize Lync 2010, then you should not proceed with the initialization and sign-in process.

Prerequisites

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

Important note Important

Before starting this walkthrough, complete the following tasks.

  • Create an event handler for the StateChanged and CredentialRequested events.

  • Create an asynchronous callback method with the signature of the System.AsyncCallbackdelegate that is invoked by the client upon completion of the initialize operation.

Initialize the Client

  1. 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 . For more information, see Walkthrough: Start a New Application .

  2. Register for the StateChanged and CredentialRequested events for this LyncClient instance.

    Tip Tip

    LyncClient is derived from Client , which means the StateChangedevent can be registered using LyncClient.

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

    If the property value is true , you must initialize the client.

  4. Get the current client state and call into BeginInitialize if the client state is ClientState . Uninitialized.

    Important note Important

    If the client state is not ClientState . Uninitializedthen a different process has already initialized Lync 2010. Custom applications designed to have exclusive use of Lync 2010 should exit at this point.

  5. In the initialization callback method you defined, read the System.IAsyncResult. IsCompleteproperty.

    If the value of the property is true then the initialize operation is complete.

  6. Handle the client state changed event.

    If the new client state is ClientState . SignedOut, Call the BeginSignIn method. When UI Suppression is enabled, the user credential parameters should not be null. When required credentials are not passed in the sign in method, the operation raises the CredentialRequested event. You call Submit , passing user credentials in the first two arguments of the method. The sign in process completes when acceptable credentials have been submitted.

    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 in its place. It is critical that you do not ignore the return value ( System.IAsyncResult) if you intend to call EndSignIn on your UI thread instead of inside a callback passed into BeginSignIn . For more information about callback delegates, see Asynchronous Programming .

  7. Handle the CredentialRequested event by gathering the user's credentials and supplying them to the client.

    This event is raised when you have called BeginSignIn with null values in the first three arguments.

  8. Handle the client state changed event.

    If the new client state is ClientState.SigningIn, then Lync 2010 is completing the user sign-in process. You may experience a delay of several seconds depending on the current level of network traffic.

    When the user has been signed in, you receive a client state changed event indicating the current client state is ClientState.SignedIn.

Examples

The following examples initialize and sign a user in to a Lync 2010 instance that runs with a suppressed UI.

Initialize Callback Method

The following example is invoked by the client when the initialize operation is complete.

C#  Copy imageCopy Code
		/// <summary>
		/// callback method called by LyncClient.SignIn()
		/// </summary>
		/// <param name="source">LyncClient</param>
		/// <param
name="_asyncOperation">IAsynchronousOperation
callback</param>
		/// 
		private void SigninCallback(IAsyncResult ar)
		{
			if (ar.IsCompleted == true)
			{
				try
				{
					((LyncClient)ar.AsyncState).EndSignIn(ar);
			}
				catch (RequestCanceledException re)
				{
					MessageBox.Show("Request canceled exception: "
+ re.Message);
			}
		}
		
	}
		/// <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">IAsynchronousOperation. 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);
		}
	}


ClientStateChanged Event Handler

C#  Copy imageCopy Code
		/// <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 _Client_ClientStateChanged(Object source,
ClientStateChangedEventArgs data)
		{
			if (ClientStateChangedEvent != null)
			{
				if (data.NewState == ClientState.SignedIn)
				{
					MessageBox.Show("Signed in");
			}
				if (data.NewState == ClientState.SignedOut)
				{
					((LyncClient)source).BeginSignIn(null, null,
null, SigninCallback, source);
			}
		}
	}

Credentials Request Event Handler

The following example obtains the local users credentials and passes them to the client.

C#  Copy imageCopy Code
		void _LyncClient_CredentialRequested(object sender,
CredentialRequestedEventArgs e)
		{
			if (e.Type == CredentialRequestedType.SignIn)
			{
				e.Submit("FranckH@contoso.com", "Frank_Password",
false);
		}
	}

Initialize the Client

The following example registers for state change events on the client, checks the current state of the client, and initializes the client if not initialized.

The code in this example is appropriate for applications designed to have exclusive access to Lync 2010.

C#  Copy imageCopy Code
		/// <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.InSuppressedMode == true)
				{
					if (_LyncClient.State ==
ClientState.Uninitialized)
					{
						Object[] _asyncState = { _LyncClient };
					 
_LyncClient.BeginInitialize(InitializeCallback, _asyncState);
				}
			}
				_LyncClient.SignInDelayed +=
_LyncClient_SignInDelay;
				_LyncClient.StateChanged +=
_Client_ClientStateChanged;
				_LyncClient.CredentialRequested += new
EventHandler<CredentialRequestedEventArgs>(_LyncClient_CredentialRequested);
		}
			catch (NotStartedByUserException h)
			{
				throw new ClientWrapException(h, "Lync is not
running", true);
		}
			catch (Exception ex)
			{
				throw new ClientWrapException(ex, "General
Exception", true);
		}
	}


Next Steps

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

See Also

Concepts

Other Resources

Walkthrough: Start a New Application