In SharePoint 2010 you have Client side object model. Now you can write code/script against share point from client machine. In Client Object Model you have two certain options like ECMA script, Client Object managed code Model and Silver light. You can fetch/add/update data from list, library. Also you can get site related information.

In this article we will see how to get information of user from the user info list at client side.  We can get user information like login name, email address, mobile number, office phone number, note of OC, etc.

Using ECMA java scripts we can do that at client side. For that you have to refer the SP.js file, it’s been already referred in master page by default.

To access user information you need to follow some steps like loading current SP context, getting list object, loading list, populating list, etc.

We will go through all steps with the code snippet.

In following example we’ll fetch user’s information with the help of ID of user.

Step 1: Get the current client context

Get the client context and the web object as follows,

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

       this.web = clientContext.get_web();

        clientContext.load(this.web);

now next, need to  call load method on the clientContext object, then only context is ready to use.

Step 2: Write CAML query to get user item

Now we will write the CAML query to fetch the user information or item. Here we are using “ID” to get specific record.

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' +'<Value Type=\'Number\'>' + userID + '</Value></Eq>' +'</Where></Query><RowLimit>1</RowLimit></View>');

Step 3: Get the User Info List

Now we will write code to get the user info list. Here the way to get user info list is different. SharePoint has provided a method to get that list.

var userInfoList = this.web.get_siteUserInfoList();

 

 

 Step 4: Get the user information (list item)

Following code will return the user item from the user info list which contains user information.

this.collListItem = userInfoList.getItems(camlQuery);

clientContext.load(collListItem);

Step 5: Call executeQueryAsync

This is very important step in the ECMA script. You need to pass method as parameter to this method. Its’ as follows,

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));

And method body are as follows,

function onQuerySucceeded(sender, args)

{

var item = collListItem.itemAt(0);

var userName = item.get_item('UserName');

alert(userName);

var name = item.get_fieldValues().Name;

alert(name);

}

Step 6: Get other information

You can also get other useful information using the “get_fieldValues()” method on the item object.  Code will be like,

Get Name:

var obj =  item.get_fieldValues().Name;

Get UserName:

var obj =  item.get_fieldValues().UserName;

 

Get Email:

var obj = item.get_fieldValues().Email;

Get First Name:

var obj = item.get_fieldValues().FirstName;

Get Job Title:

var obj = item.get_fieldValues().JobTitle;

Get Mobile Phone:

var obj = item.get_fieldValues().MobilePhone;

Get Work Phone:

var obj = item.get_fieldValues().WorkPhone;

Get Notes:

var obj = item.get_fieldValues().Notes;

Get Picture:

var obj = item.get_fieldValues().Picture;

Get SIP Address:

var obj = item.get_fieldValues().SipAddress;

Get Title:

var obj = item.get_fieldValues().Title;

Get  Department:

item.get_fieldValues().Department;

Get  Web Site:

item.get_fieldValues().WebSite;

Get TimeZone:

item.get_fieldValues().TimeZone;

So, in this way we can get the information of user.