1

I'm trying to get the current user's profile picture and I'm using CSOM.

But I receive an Access Denied error, when I actually gave Read permission to user profile service.

$(document).ready(function () {

    SP.SOD.executeOrDelayUntilScriptLoaded(GetCurrentUser, 'SP.UserProfiles.js');

});

function GetCurrentUser() {

    var userProfileProperties;
    var deferred = $.Deferred(function () {
        var context = SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(context);
        userProfileProperties = peopleManager.getMyProperties();
        context.load(userProfileProperties, 'DisplayName', 'Email');
        context.executeQueryAsync(function () { deferred.resolve(userProfileProperties) }, function (sender, args) { deferred.reject(sender, args) });
    });

    deferred.done(function (userProfileProperties) {

        var userName = userProfileProperties.get_displayName();
        var email = userProfileProperties.get_email();
        var picUrl = QS('SPHostUrl') + "/" + _spPageContextInfo.layoutsUrl + "/userphoto.aspx?size=S&accountname=" + email;
        $('#userimage').attr('src', picUrl);
        $('#username').text(userName);
    });

    deferred.fail(function (sender, args) {

        console.log('Error: ' + args.get_message() + '\n' + args.get_stackTrace());

    });
}
2

I am not sure that my solution will cure your Access Denied problem. You have written many lines of code to do this. I have an easy way to do this. Just make a GET request to any following URLs.

/_api/SP.UserProfiles.PeopleManager/GetMyProperties/PictureUrl

OR

/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl

I can see that you DisplayName and Email also. So your final URL should look like following

/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,DisplayName,Email

You full code should look like following if you are developing SharePoint Hosted app.

Based on our skype conversation, I found that you missed the jQuery(document).ready. So update your code as below

    jQuery(document).ready(function() {
    GetCurrentUser();

    function GetCurrentUser() {
        $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,DisplayName,Email",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function(data) {
                var userName = data.d.DisplayName;
                var email = data.d.Email;
                var picUrl = data.d.PictureUrl;
                $('#userimage').attr('src', picUrl);
                $('#username').text(userName);
            },
            error: function(error) {
                alert(JSON.stringify(error));
            }
        });
    }
});

Make sure your app has enough permission to read UserProfiles. I have following permission and I am getting result

enter image description here

  • I get nothing, no picture and no error either – Melad Batta Oct 19 '15 at 18:09
  • HTML <img id="userimage" alt="" src="" /> <div id="username"></div> – Melad Batta Oct 19 '15 at 18:11
  • Does your current user have profile picture? – Atish Dipongkor - MVP Oct 19 '15 at 18:14
  • Yes of course i've a profile picture – Melad Batta Oct 19 '15 at 18:16
  • I do not get the user name either – Melad Batta Oct 19 '15 at 18:18

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.