dinsdag 19 maart 2013

SharePoint 2010: Show lync presence in custom webpart

My customer asked for a contact details webpart with the feature if the contact is available on Lync or not. It isn’t difficult to get the contact details from a user profile in contrast to get the lync presence. I spend a few hours to get how it works in SharePoint 2010 and finally I found this blog post of Martin Kearn. With a little bit of fine tuning I rewrote the function to let it work with the UserProfile property instead of a SPFieldUserValueCollection.

The code that do the thing:

Code Snippet
  1. public static string GetPresenceHTMLFromUserProfile(UserProfile profile)
  2.         {
  3.             StringBuilder html = new StringBuilder("");
  4.             
  5.             string sipAddress = "";
  6.             //Get the sip address if it exists
  7.             if (profile[PropertyConstants.WorkEmail].Value != null)
  8.             {
  9.                 sipAddress = profile[PropertyConstants.WorkEmail].Value.ToString();
  10.             }
  11.  
  12.             //construct the html for this user and add to overall html
  13.             html.Append(String.Concat(
  14.                 "<span><a href=\""
  15.                 , Helper.GetUserProfileUrl(profile)
  16.                 , "\" id=\"ProfileLink"
  17.                 , sipAddress
  18.                 , "\">"
  19.                 , profile.DisplayName
  20.                 , "</a>"
  21.                 , "<img border=\"0\" height=\"12\" src=\"/_layouts/images/imnhdr.gif\" onload=\"IMNRC('"
  22.                 , sipAddress
  23.                 , "')\" ShowOfflinePawn=\"1\" style=\"padding-left: 5px;\" id=\"PresencePawn"
  24.                 , sipAddress
  25.                 , "\" alt=\"presence pawn for "
  26.                 , sipAddress
  27.                 , "\"/></span>"
  28.             ));
  29.             
  30.             return html.ToString();
  31.         }

2 opmerkingen: