Visit our SharePoint Forum

SharePoint developer? Submit Yourself as Freelancer

Tuesday, July 20, 2010

Programmatically access user profile + client object model SharePoint 2010

Here is a Code snippet for retrieving user profile picture using Client Object model – ECMAScript . I am passing the userId from the front end to a javascript method called “getUserProfile()” to retrieve the user profile info. The method onQuerySucceeded will get you the user Profile info.

function getUserProfile(userID)
{
var clientContext = new SP.ClientContext.get_current();

var web = clientContext.get_web();

var userInfoList = web.get_siteUserInfoList();

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>’);

this.collListItem = userInfoList.getItems(camlQuery);

clientContext.load(collListItem);

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

function onQuerySucceeded(sender, args)
{

var item = collListItem.itemAt(0);

var profile = item.get_item(‘Notes’);

var pictureUrl = item.get_item(‘Picture’).get_url();

var userImage = document.getElementById(‘myImageContainer’); -> Image object

userImage.src = pictureUrl;

var profileDiv = document.getElementById(‘userProfileContainer’);

profileDiv.innerHTML = profile;
}

5 comments:

Anonymous said...

I want to update User profile of other users using JQuery… I am able to update mine…. how should I do it….. do we have option of elevated privelages in Client Object Model specially ECMA Script / JQuery?

sgaurav said...

Able to find way.... thx

Clem said...

this code is shown everywhere on the net but no one can do it with a login account id (domain\user) or name. Only the number that sharepoint uses internally. This is not available most of the time.

Anonymous said...

Agree with what Clem says above, using the user id is problematic. How do I get this user id if I only have the login?

electronic signatures said...

Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!

SharePoint Programming