Pages

Tuesday, June 5, 2012

How to 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 users 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.
public class UserEntity
{
    public BitmapImage UserImage { get; set; }
}

Here is the xaml for Silverlight control , Binding is added as BitMapImage stream

<Image Name="userImage" Source="{Binding Path=UserImage}" Margin="2,2,2,2" Height="40" Width="40"></Image>

Now the function to get user image

public partial class MainPage : UserControl
{
  List<UserEntity> allUsers = new List<UserEntity>();
  LyncClient client;
    public MainPage()
  {
    InitializeComponent();
    client = LyncClient.GetClient();
   
    UserEntity userObjet = new UserEntity();

    if (client != null)
    {                    
       ContactManager cManager = client.ContactManager;
       if (cManager != null)
       {
         Contact contact = cManager.GetContactByUri("useremail@domain.com");
         if (contact != null)
         {
           List<ContactInformationType> ciList = new List<ContactInformationType>();
           ciList.Add(ContactInformationType.Photo);
           IDictionary<ContactInformationType, object> dic = null;
           dic = contact.GetContactInformation(ciList);
           if (dic != null)
           {
             Stream photoStream = dic[ContactInformationType.Photo] as Stream;
             if (photoStream != null)
             {
               BitmapImage userImageBitMap = new BitmapImage();
               userImageBitMap.SetSource(photoStream);
               userObjet.UserImage = userImageBitMap;
             }
           }
         }
      }
    }
         allUsers.Add(userObjet);
  if (allUsers != null && allUsers.Count > 0)
         {
           userImage.ItemsSource = allUsers;
  }
  }

No comments:

Post a Comment