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 { get; set; }
  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. UserEntity userObjet = new UserEntity();
  10. if (client != null)
  11. {
  12. ContactManager cManager = client.ContactManager;
  13. if (cManager != null)
  14. {
  15. Contact contact = cManager.GetContactByUri("[email protected]");
  16. if (contact != null)
  17. {
  18. List<ContactInformationType> ciList = new List<ContactInformationType>();
  19. ciList.Add(ContactInformationType.Photo);
  20. IDictionary<ContactInformationType, object> dic = null;
  21. dic = contact.GetContactInformation(ciList);
  22. if (dic != null)
  23. {
  24. Stream photoStream = dic[ContactInformationType.Photo] as Stream;
  25. if (photoStream != null)
  26. {
  27. BitmapImage userImageBitMap = new BitmapImage();
  28. userImageBitMap.SetSource(photoStream);
  29. userObjet.UserImage = userImageBitMap;
  30. }
  31. }
  32. }
  33. }
  34. }
  35. allUsers.Add(userObjet);
  36. if (allUsers != null && allUsers.Count > 0)
  37. {
  38. userImage.ItemsSource = allUsers;
  39. }
  40. }