This topic demonstrates how to get an individual contact by Uri or search for a contact by telephone number. This second search method is known as a "reverse lookup".

Get a Contact by URI

If you have the URI of the contact you want, simply call into GetContactByUri and pass the Uri of the user you want to call. If you want continual updates to the contact's information, add the Contact returned to a ContactSubscription instance and then call into Subscribe on the subscription.

Look Up a Contact by Other Attributes

You can perform a reverse search for a contact using the primary and secondary work telephone number for the user. The search succeeds if the telephone number string you provide resolves to a telephone number in the list of telephone numbers published by the contact. The result of the search operation is returned asynchronously in a callback method. If you pass the email address, display name, primary or secondary telephone number of the contact you seek into BeginLookup , EndLookup returns a Contact that represents the user.

Examples

Get a Contact by URI

The following example adds a Contact instance to a previously instantiated Subscribe instance and then subscribes.

C#  Copy imageCopy Code
 
//Class field
private ContactSubscription foundContacSubscription =
_CommuicatorClient.ContactManager.CreateSubscription();

	 /// <summary>
		/// Searches for contacts or distribution groups using the
specified method
		/// </summary>
		/// <param name="searchName">string. Search
string.</param>
		/// <param name="searchMethod">string. Search
method.</param>
		public void SearchForGroupOrContact(string searchName)
		{
			Contact foundContact =
_CommuicatorClient.ContactManager.GetContactByUri(searchName); 
			string resultString =
foundContact.GetContactInformation(ContactInformationType.DisplayName).ToString()
+ ", " +
foundContact.GetContactInformation(ContactInformationType.PrimaryEmail).ToString()
+ Environment.NewLine;
			Console.WriteLine(resultString);
			foundContacSubscription.AddContact(foundContact);

			List<ContactInformationType> subscribeTypeList =
new List<ContactInformationType>();
		 
subscribeTypeList.Add(ContactInformationType.Availability);
		 
foundContacSubscription.Subscribe(SubscriptionRefreshRate.High,subscribeTypeList);
	}

Search for a Contact by Telephone Number

The BeginLookup method is asynchronous and may block your application for several seconds. Whether you obtain the result of the lookup by calling EndLookup on your UI thread or within a callback method invoked by the Lync thread, you cannot begin the call until the contact result is returned.

C#  Copy imageCopy Code
		/// <summary>
		/// Called on the LyncClient thread by the
ConversationManager instance when a conversation has been
		/// added to the Conversations collection on
ConversationManager.
		/// A conversation is added when
ConversationManager.AddConversation() is called on the UI thread or
when a remote user
		/// is calling the local user.
		/// </summary>
		/// <param name="sender">object. A
ConversationManager instance.</param>
		/// <param name="e">ConversationManagerEventArgs. The
event state data object.</param>
		void ConversationManager_ConversationAdded(object sender,
ConversationManagerEventArgs e)
		{
			e.Conversation.ParticipantAdded +=
Conversation_ParticipantAdded;
			if
(e.Conversation.Modalities[ModalityType.AudioVideo].State !=
ModalityState.Notified)
			{
				if (e.Conversation.State !=
ConversationState.Active)
				{
					IAsyncResult ar =
_LyncClient.ContactManager.BeginLookup("Jay Hamlin",
ContactManagerCallback, e.Conversation);
			}
		}
	}

The following example handles the asynchronous callback method invoked by the BeginLookup call when the BeginLookupoperation is complete.

C#  Copy imageCopy Code
		/// <summary>
		/// Called by Lync thread when lookup operation completes
		/// </summary>
		/// <param name="ar">IAsyncResult. the result of the
operation</param>
		private void ContactManagerCallback(IAsyncResult ar)
		{
			try
			{
				Conversation newConversation =
(Coversation)ar.AsyncState;
				object returnValue =
_ClientModel._LyncClient.ContactManager.EndLookup(ar);
				if (returnValue.GetType().FullName =="ContactEndpoint")
				{
					ContactEndpoint foundEP = returnValue as
ContactEndpoint;
					newConversation.AddParticipant(foundEP);
			}
				else
				{
					Contact foundEP = returnValue as Contact;
					newConversation.AddParticipant(foundEP);
			}
		}
			catch (ItemNotFoundException inf)
			{
				newConversation.End();
				MessageBox.Show("The lookup could not find a
contact: " + inf.Message);
		}
	}

		/// <summary>
		/// ParticipantAdded callback handles ParticpantAdded event
raised by Conversation
		/// </summary>
		/// <param name="source">Conversation Source
conversation.</param>
		/// <param name="data">ParticpantCollectionEventArgs
Event data</param>
		void Conversation_ParticipantAdded(object source,
ParticipantCollectionChangedEventArgs data)
		{
			if (data.Participant.IsSelf == true)
			{
				if
(((Conversation)source).Modalities[ModalityTypes.AudioVideo].CanInvoke(ModalityAction.Connect))
				{
					object[] _asyncState = {
((Conversation)source).Modalities[ModalityTypes.AudioVideo],
"CONNECT" };
					try
					{
					 
((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityConnectOptions.None,
ModalityCallback, _asyncState);
				}
					catch (LyncPlatformException ce)
					{
						throw new Exception("Lync Platform
Exception on BeginConnect: " + ce.Message);
				}
			}

		}
			else
			{
				if
(((Conversation)source).Modalities[ModalityTypes.AudioVideo].CanInvoke(ModalityAction.Connect))
				{
					object[] _asyncState = {
((Conversation)source).Modalities[ModalityTypes.AudioVideo],
"CONNECT" };
				 
((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityConnectOptions.IncludeAllContexts,
ModalityCallback, _asyncState);
			}
		}
	}