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;
  }
  }

Monday, June 4, 2012

How to Initialized Client Context in Office 365 / Claim Based Authenticated Sites

Problem:

Generally we initialize the client context straight away by creating new object of class ClientContext and by providing the url of the site as well as needed credentials, but for sites using claims based authentication (let it be Office 365 site or On Premise site) this logic won’t, so what’s the solution?

Solution:
User must be logged in to the Federated Authentication Provider before initializing the client context object and hence there should be some provision in the client application to let users log in.
Microsoft has released a sample console application which demonstrates about remote authentication in SharePoint Online / Claim based SharePoint sites using managed client object model.

Hope this helps someone.