The User Information List – a really valuable resource!

The User Information List – a really valuable resource!

The SharePoint ‘User Information List’ is an often ignored hidden SharePoint list. When a user is added to a SharePoint site their details are copied into the list. It’s really easy to forget its there and it can be a valuable resource when developing in the sandbox.

The other day whilst working on a sandboxed web part I needed to retrieve a link to a users My Site picture and the URL to their My Site itself. I went straight into C# server side and wrote the code to do the job. This then made my web part into a farm solution (because I was using the Microsoft.Office.Server namespace) rather than a sandbox one meaning this wasn’t such a great solution!

It then occurred to me that I could query to the User Information List to get the data I needed – the only caveat here is that the user had to be a member of the site but was willing to accept that.

The great thing about this list is that because it is just a standard SharePoint list we query it from the Client Object Model – ECMAScript in my case. All that is required is some standard ECMAScript code with a CAML query to query items in the list based on your criteria.

Here is an example of said code to retrieve a list item from the User Information List -

[code lang="js"]

function getUserDetails() {

var ctx = new SP.ClientContext.get_current();

userInformationList = ctx.get_web().get_lists().getByTitle("User Information List");

var query = new SP.CamlQuery();
var queryText = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Name'/><Value Type='Text'>iddev\Alex</Value></Eq></Where></Query></View>";
query.set_viewXml(queryText);

userItems = userInformationList.getItems(query);

ctx.load(userItems);

ctx.executeQueryAsync(Function.createDelegate(this, getUserDetailsSuccess),
Function.createDelegate(this, getUserDetailsFailure));

}

[/code]

You will see here I am hardcoding the value here ‘iddev\Alex’ for test purposes. The best thing to do would be to pass it as a parameter to the function. In our success method ‘getUserDetailsSuccess’ we can then retrieve the values we require from the list item. In this case I will be getting the URL to the user’s My Site picture thumbnail.

[code lang="js"]

function getUserDetailsSuccess(sender, args) {

var listEnumerator = userItems.getEnumerator();

while (listEnumerator.moveNext()) {

var picUrl = listEnumerator.get_current().get_item("Picture");
// Do something with the returned picture url
}
}

[/code]

As you can see retrieving items from the User Information List is the same as any other SharePoint list and helps to keep your code sandboxable. As for generating your CAML query and experimenting with querying the list I find U2U’s CAML Builder is the best tool for the job!

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">