0
public static string GetUserProfilePictureURL(string username)
{
    string Url = string.Empty;

    SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
    UserProfileManager upm = new UserProfileManager(serviceContext);
    if (upm.UserExists(username))
    {
        UserProfile profile = upm.GetUserProfile(username);
        if (profile != null)
        {
            Url = profile["PictureURL"].Value.ToString();
        }
    }

    //change it to the default if the user does not have an image specified
    if (string.IsNullOrEmpty(Url))
        Url = "/_layouts/15/userphoto.aspx?size=S&accountname=";

    return Url;
}

How could i do this but using javascript?

0
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://yoursite.sharepoint.com/sites/test/SiteAssets/imgToSetAsProfilePic.jpg", true);
xhr.responseType = "arraybuffer";
xhr.onload = function (event) {  
  var imageAsArrayBuffer = xhr.response;  
  if (imageAsArrayBuffer) {         
    jQuery.ajax({
            url:_spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMyProfilePicture",
            type: "POST",
            data: imageAsArrayBuffer,
            processData: false,
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "content-length": imageAsArrayBuffer.byteLength
            },
            success: function (data) { 
                console.log(data);
                alert("profile pic set");
            },
            error: function (err) {
                console.log(err);
                alert("error");
            }
        });
  }
}; 
xhr.send(null);

Source - http://www.vrdmn.com/2013/11/set-userprofile-picture-using-net.html

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.