Using the SharePoint 2010 Client Object Model to return user information

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

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

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