July 23
Accessing the User Profile in both MOSS and WSS
A subset of the user profile, including the "About Me" description and picture, is available in the AllUserAccounts list of the site collection's root site. This is common to both "pure" WSS installations and MOSS installations, where this information is rendered and edited in the user's My Site. This includes the fields: Account ("Title" field, this is the windows account), Name, E-Mail, About Me, Picture, Department, Job Title and SIP Address. In MOSS, the MOSS profile will get synced back to this list.
The list is exposed as SPContext.Current.SiteUserInfoList. It is the list of the "User Info" collection of the Site Collection that contains the current web site.
To access this, create an SPQuery against the SPSite.AllUserAccounts list with either the user ID, user account, or email address.
The cool thing about this is you can use this information to render your own user information throughout your web part applications (that would be AJAX web part applications, preferably!), whether you're running full-blown MOSS or just WSS. You can also use the user's ID in the site to build a link to the user's profile page, whether it's the WSS site profile page or the full My Site profile in a MOSS installation. The profile data actually gets pulled back to the WSS user list in a job, so it may not update right away. At first I thought it was broken, but sometimes it just takes a while to sync.
Here's how we're using this at NewsGator: In NewsGator Social Sites, we expose this mini-profile info as a read-only XML endpoint so we can render that information throughout our portal applications wherever we want to display contextual user information. This is the preferred architecture for AJAX applications-- loosely coupled endpoints are exposed and secured, and are available to pull into arbitrary AJAX controls at whim. Currently, I'm working on a user profile control that mashes up SharePoint information as well as custom "social computing" data from both NewsGator data sources and SharePoint data sources. With this, we're exposing the power of the My Site profile directly into the context of the collaborative sites. You can use this technique in your own apps to access profile data in code that runs in both MOSS and WSS.
Here's a code sample of accessing a user from the profile list, where userID is the WSS user id of the user (like, "5"). You could also use something like the login name or email address in the query.
SPWeb web = SPContext.Current.Web;
SPList userList = web.SiteUserInfoList;
SPQuery query = new SPQuery();
query.ViewFields =
@"<FieldRef Name='Title'/><FieldRef Name='Name'/><FieldRef Name='Notes'/><FieldRef Name='Picture'/><FieldRef Name='Email'/><FieldRef Name='ID'/>";
query.Query = string.Format(
@"<Where><Eq><FieldRef Name='ID'/><Value Type='Int'>{0}</Value></Eq></Where>", userID);
SPListItemCollection items = userList.GetItems(query);