Lync State display in a custom webpart for SharePoint 2010

I recently spent a while trying to integrate the Lync state into a custom web part and found it a little tricky so wanted to share my learning's. With SharePoint 2010's new social capabilities, I see this integration as being a must-have for any webpart or other custom interface that refers to people.

How does SharePoint do it?

Whenever a person's name is mentioned in the standard SharePoint UI, if the user has Office Communicator installed, the name will be shown with the Office Communicator 'pawn'. The pawn shows the user's status and gives them access to the pop-up menu to see more details.

Depending on which version of Office the user has installed, the experience will vary, however it will be the same experience as seen in Outlook. In Office 2010 it is a square icon with a richer drop-menu, in 2007 it is round.

SPPresencePawn

In addition to the pawn, the user's name will be a hyperlink to their profile page. This will vary depending on whether the user is in the main 'User Profile Service Application' (or 'profile database' for old-school terminology) or not. If the user does have a profile then the hyperlink will redirect to the user's main profile page under My Site. If the user does not have a profile, the hyperlink will redirect to the SharePoint Foundation 'User Information' page. All users in a site have a 'User Information' page, if they also have a User Profile the settings from the profile are synchronised down to the User Information page on a scheduled basis (by a timer job).

The key piece of information to make the presence work is the user's SIP Address which is basically their IM address (This is not always the same as email address). When a users is either added to a site in SharePoint or has their profile imported, the SIP Address will be drawn from Active Directory which is where OCS stores it and placed into the SIP address field in either the user's profile which will in turn synchronise down to the site's local 'User Information' page.

How does the pawn work in terms of HTML?

The pawn is basically an IMG element which has "IMNRC('[user's SIP Address]')" for the onload function. This will user client-side script that is part of Office to load the presence pawn on the page.

The hyperlink on the user's name is just a simple A element which redirects to "/_layouts/userdisp.aspx?ID=[User's local user information list item ID]". This userdisp.aspx page will then redirect to the user's main profile page if they have a profile, otherwise it will display the basic information that the user information list item stores.

How to get the SIP Address?

Hopefully you'll have spotted by now that the presence pawn relies on the user's SIP Address. To get the SIP address you need to get the user's User Information list item from the local site's (SPWeb to be precise) SiteUserInfoList which is basically a hidden SPList that is in every web.

There is a handy property to get you to this list called 'SPWeb.SiteUserInfoList'. This will give you an SPList object which represents the User Information list. From here you just need to find the item that represents the user you wish to display. The best way to do this is via their ID (the ID of the list item) by calling SPWeb.SiteUserInfoList.GetItemById([int User's ID]), however you can also use a variety of other methods which use SPQuery or match a specific field to a value.

In most scenarios, you may be getting the user information from a SPFieldUserValueCollection which is basically the field type that is used for 'Person' fields. If this is the case you can use SPFieldUserValue.LookupId to get the ID of the user's User Information list item.

Putting it all together

This code sample is a method that accepts an SPFieldUserValueCollection and SPSite as inputs and returns back the HTML for displaying each entry in the SPFieldUserValueCollection with the presence pawn and link to their profile. This will be presented exactly as SharePoint presents it by default. This could be extended to use ALT tags in the IMG and A elements.

I then simply add the HTML to an HtmlWriter or in my case a TableCell.Text property to display it on screen.

I've take a few extra steps here by adding ID and alt tags and trying to make the code readable, but I'm sure you get the idea:

public string GetPresenceHTMLFromUVC(SPSite thisSite, SPFieldUserValueCollection uvc)
{
StringBuilder html = new StringBuilder("");
using (SPWeb thisWeb = thisSite.RootWeb)
{
foreach (SPFieldUserValue uv in uvc)
{
string sipAddress = "";
//Get the sip address if it exists
SPListItem user = thisWeb.SiteUserInfoList.GetItemById(uv.LookupId);
if (user["SipAddress"] != null)
{
sipAddress = user["SipAddress"].ToString();
}
//construct the html for this user and add to overall html
html.Append(String.Concat(
"<div id\"PresenceLink"
, sipAddress
, "\">"
, "<img border=\"0\" height=\"12\" src=\"/_layouts/images/imnhdr.gif\" onload=\"IMNRC('"
, sipAddress
, "')\" ShowOfflinePawn=\"1\" style=\"padding-right: 3px;\" id=\"PresencePawn"
, sipAddress
, "\" alt=\"presence pawn for "
, sipAddress
, "\"/>"
, "<a href=\""
, thisWeb.Url
, "/_layouts/userdisp.aspx?ID="
, uv.LookupId
, "\" id=\"ProfileLink"
, sipAddress
, "\">"
, uv.User.Name
, "</a></div>"
));
}
}
return html.ToString();
}