Get user Picture using Lync APIs

Get user picture using Lync APIs

I was exploring the Lync APIs after installing Lync SDK on my machine and as a curiosity tried to display the user’s communicator / Lync Image in custom control.

To get the following example working - You will need to install the Lync SDK or add the references of Lync APIs to your project. Lync SDK can be downloaded here , after this – add the references from location [%root%]\Program Files (x86)\Microsoft Lync\SDK\Assemblies\Desktop\Microsoft.LyncModel.dll to your project.

I created one sample Silverlight project where I took Silverlight image control and tried to provide the source. This control takes source as either full path of image or you can assign the BitMap Imag stream.

I created one entity class named – UserEntity which holds BitMapImage as a property.

  1. public class UserEntity  
  2. {  
  3.    public BitmapImage UserImage { getset; }  
  4. }  
Here is the xaml for Silverlight control - Binding is added as BitMapImage stream.
  1. <Image Name="userImage" Source="{Binding Path=UserImage}" Margin="2,2,2,2" Height="40" Width="40"></Image>  
Now the function to get user image.
  1. public partial class MainPage : UserControl  
  2. {  
  3.   List<UserEntity> allUsers = new List<UserEntity>();  
  4.   LyncClient client;  
  5.     public MainPage()  
  6.   {  
  7.     InitializeComponent();  
  8.     client = LyncClient.GetClient();  
  9.      
  10.     UserEntity userObjet = new UserEntity();  
  11.   
  12.     if (client != null)  
  13.     {                      
  14.        ContactManager cManager = client.ContactManager;  
  15.        if (cManager != null)  
  16.        {  
  17.          Contact contact = cManager.GetContactByUri("[email protected]");  
  18.          if (contact != null)  
  19.          {  
  20.            List<ContactInformationType> ciList = new List<ContactInformationType>();  
  21.            ciList.Add(ContactInformationType.Photo);  
  22.            IDictionary<ContactInformationType, object> dic = null;  
  23.            dic = contact.GetContactInformation(ciList);  
  24.            if (dic != null)  
  25.            {  
  26.              Stream photoStream = dic[ContactInformationType.Photo] as Stream;  
  27.              if (photoStream != null)  
  28.              {  
  29.                BitmapImage userImageBitMap = new BitmapImage();  
  30.                userImageBitMap.SetSource(photoStream);  
  31.                userObjet.UserImage = userImageBitMap;  
  32.              }  
  33.            }  
  34.          }  
  35.       }  
  36.     }  
  37.     allUsers.Add(userObjet);  
  38.   if (allUsers != null && allUsers.Count > 0)  
  39.          {  
  40.            userImage.ItemsSource = allUsers;  
  41.   }  
  42.   }