3

In the SharePoint application, we have a left menu in which we are showing current user profile picture. In order to get the user profile image, we are using SharePoint client object model using below code.

var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties()
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onSuccess, onFail);

function onSuccess() {
var up_url = userProfileProperties.get_pictureUrl();
if (up_url !== null) {
    $('.profile-image img').attr('src', up_url);     
}
}

function onFail(sender, args) {
 alert("Error");
}

But each time user login to site, it asking for credentials for image.

2

We fixed the issue by using below code

var userpicUrl = '/_layouts/15/userphoto.aspx?accountname=' + userProfileProperties.get_accountName() + '&size=M&url=' + userProfileProperties.get_pictureUrl();
$('.profile-image img').attr('src', userpicUrl );
1

There doesn't seem to be anything wrong with your code from the surface with a read through.

So it could be that you need to add your site to the safe zones of your browser. Have you checked this in other browsers?

If this is the case for IE go to Tools > Internet Options > Security > Local Intranet > Sites > Advanced > Then add your site here.

If this does work then if you are a domain Admin you can change the policy globally.

However if this is not the case then it leads me to say you would need to debug this with fiddler. You can then ascertain the reason behind the multiple authentication.

0

Code seems to be ok. But I suggest you put alert after each line and check the code again you will not get alert if any line is throwing error.

Best way to debug on client side.

  • We are not getting any error from client object code. Instead its asking for user credentials again while fetching image src from user profile. – dotnetmirror.com May 15 '13 at 5:50
0

surely It will work

function setUserProfilePicture() {
    var userpicUrl = '/_layouts/15/userphoto.aspx?accountname=' + userProfileProperties.get_accountName() + '&size=M&url=' + userProfileProperties.get_pictureUrl();
    var parentsite = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;

    userpicUrl = parentsite + userpicUrl;
    var ProfileUrl = userProfileProperties.get_userUrl();
    document.getElementById("profilelink").href = ProfileUrl;
    $('.profile-image img').attr('src', userpicUrl);
    var Placeholder = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/images/PersonPlaceholder.42x42x32.png"
    document.getElementById("profileimage").src = Placeholder;


    if (userpicUrl !== null) {
        $('.profile-image img').attr('src', userpicUrl);
        document.getElementById("profileimage").src = userpicUrl;
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.