Freigeben über


Office Communicator integration (Presence) in a custom webpart for SharePoint 2010

I recently spent a while trying to integrate the Office Communicator presence pawn 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();
}

This article was published by

MartinKearn

Martin Kearn Senior Consultant Microsoft Consulting Services UK Martin.Kearn@Microsoft.com

Click here for my bio page

Please feel free to contact me directly via email or via the blog comments if you have something to say about this article.

Comments

  • Anonymous
    May 07, 2010
    Martin, really nice reuse of the already existing functionality in Sharepoint 2010. Regards, Marc

  • Anonymous
    May 31, 2010
    This is just showing the unknown icon.. not displaying the gree icon in the wbpage.. can u pls assist..

  • Anonymous
    May 31, 2010
    This is just showing the unknown icon.. not displaying the gree icon in the wbpage.. can u pls assist..

  • Anonymous
    June 02, 2010
    Please send your code and a screen shot to me and I';ll have a look

  • Anonymous
    June 09, 2010
    Great Post, do you have this packaged into a solution?

  • Anonymous
    July 08, 2010
    What if you were trying to expose the OC pawn on a static .aspx page (not drawing from a webpart) for a particular contact person?

  • Anonymous
    October 05, 2010
    My Problem is every time i edit the html source of a webpart and put in your code, sharepoint loses the "onload" part in the img tag, so its only the grey icon shown :/ And another question: Is there a way in getting the online status of a user server side, so that a user doesnt need a running communicator ? Keep up the good work regards Johannes

  • Anonymous
    January 04, 2011
    There is no need to dispose of rootweb anymore (blogs.msdn.com/.../sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx) . Just FYI.

  • Anonymous
    January 07, 2011
    I am not a developer guy, so I am wondering where I put this code into. Please excuse my ignorance. Thanks

  • Anonymous
    August 03, 2011
    Nice Article Martin.. Thanks for Sharing ..

  • Anonymous
    August 22, 2011
    Interesting info.  I need to try this out.  I'm curious if you know the answers to any of these questions:

  • Do you have to have the Lync client installed for this like you do with the Silverlight controls?  
  • Any idea if this works with Office 365?  
  • Do you know if it will work on non-windows platforms?   Thanks
  • Anonymous
    November 14, 2011
    This is a very nice approach but this gets very slow when displaying more than say 300 users on the same page like in a filtered phonebook scenario.  Do you have any ideas about that?

  • Anonymous
    January 24, 2012
    If the Link icon is not showing please check the SIP address in the User Profile Properties

  • Anonymous
    February 03, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 31, 2012
    The comment has been removed

  • Anonymous
    September 17, 2012
    Hi, does this approch work on chrome browsers as well, the default sharePoint page does not show presence in chrome browsers. Thanks, Pruthvi

  • Anonymous
    February 25, 2013
    I am not a programmer, but preparing a basic html page for my team. I want to show communicator status on html page. do we have any reference code for it

  • Anonymous
    April 09, 2013
    hi, I am new in share point development but I have created a web part to show the communicator on the page. I have referenced you code and my web-part only shows the users on the server. Can you please guide me how can I achieve the task? Here is a snap shot of my web part : s11.postimg.org/.../webpart.jpg What am I missing? please reply ASAP.

  • Anonymous
    April 09, 2013
    Hi, I have solved the previous problem but now I have a new one.. when I point on the chat icon on the pawn a message is shown 'to use this feature, log in to an instant messaging  program'. which messenger program it is talking about??

  • Anonymous
    May 11, 2014
    Hi, The code above is not working on Chrome/Firefox. Is there any thing do we need to add/edit?

  • Anonymous
    July 12, 2014
    the above code works upto getting the user, but I am able construct the html tag inside the c#. please give some inside on to this.