Dev Notes

Notes on Development with Microsoft Technologies

Using the SharePoint 2010 Client Object Model to return user information

2 Comments

In a recent project, I needed a way to quickly get some additional details about a user from the user information list. The project is making use of search and in the core results the only item being returned is the display name of the author. Since I needed to get some more details on the user, I needed a quick way to lookup the user by display name from the user information list.

The Client Object Model in SharePoint 2010 is accessible using JavaScript, so I decided that I would make use of this, coupled with jQuery, to provide the necessary details for each author returned in a set of search results. The search results page is configured to show 10 results at a time, so I know there will only be ten calls to get the information needed. Also, this search center is looking for items already contained within the specified site, so making use of the user information list is possible because the authors will have entries in the list due to their activity on the site.

First, I use jQuery to get my custom container that contains the author information. I have already modified the core results XSLT and I know the markup for the container I will be using.

function loadUserData() {
     $(".authordetails").each(function () {
          var authorName = $(this).text();
          updateAuthorDetails(authorname, this);
     });
}

The updateAuthorDetails method makes use of the SharePoint 2010 client object model to retrieve the user details. This method is passed two arguments, the name of the author (this is the display name) and the target container to update with additional details.

	function updateAuthorDetails(userName, target){
		// Use the client context to get the user.
		var clientContext = new SP.ClientContext.get_current();
		var web = clientContext.get_web();
		
		// Get the user information list for the current site
		var userInfoList = web.get_siteUserInfoList();
		
		// Define the query to get the user by the display name of the user.
		var camlQuery = new SP.CamlQuery();
		camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\'Title\'/><Value Type=\'Text\'>" + userName + "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
		
		// Load the items into a variable
		var colListItem = userInfoList.getItems(camlQuery);
		clientContext.load(colListItem);
		
		// Execute the query and set up the profile information
		clientContext.executeQueryAsync(
			function (sender, args) {
		             var item = colListItem.itemAt(0);
                             /*
                               use the list item to perform my updates 
                               to the container using jQuery
                             */
			},
			function (sender, args) {
				return null;
			});
	}

The final result of my efforts looks a little like the following:

Advertisements

Author: Chris Quick

I have been a developer of web based solutions since early 2001 delivering solutions to a wide array of organizations using ASP, ASP.NET and SharePoint. I was introduced to SharePoint in 2003 when the consulting firm I worked for at the time introduced it into the workplace. I began working with MOSS 2007 as soon as Microsoft released the RTM version in November 2006. The platform was implemented at the organization I worked for in 2007 and went live in March of that year. I was tasked with the administration and ongoing development of the platform. I currently work as a SharePoint Architect with Artis Consulting, developing solutions for a wide variety of business problems. The goal of this blog is to share my discoveries developing solutions with SharePoint. I welcome your comments and feedback to any post -- and I welcome suggestions for future topics.

2 thoughts on “Using the SharePoint 2010 Client Object Model to return user information

  1. Pingback: Governance - What It is & What It Isn't; Time to Enterprise the App Store; Will the Cloud Change Programming? - SharePoint Daily - Bamboo Nation

  2. Pingback: SharePoint Daily » Blog Archive » Governance – What It is & What It Isn’t; Time to Enterprise the App Store; Will the Cloud Change Programming?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s