Monday 1 August 2011

Get User Profile Info with Ecmascript\Javascript client Object model in SharePoint 2010


The code returns the UserProfile Picture when the userId is passed to a javascript method called “getUserProfile()”. 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;
}

Ads by Google

No comments:

Post a Comment