How can I set Set Office 365 Profile Image

Hello,

I found an article about how to upload and set Office 365 profile image using Microsoft Graph API. Can I use this code in Plumsail Forms for SharePoint? Of is there a better / other solution?

Thank you,

Daniël

(Upload And Set Office 365 Profile Image Using Microsoft Graph API - SharePoint Pals)
Upload And Set Office 365 Profile Image Using Microsoft Graph API - SharePoint Pals

Hello @danieljr,

You can try out using the PnPjs library to call the graph rest services. Find more information here:
https://pnp.github.io/pnpjs/graph/photos/

Thank you, but I didn't succeed with that information (probably i'm doing something wrong)

But I found this and think it is a good solution. But I can't get it to work either :confused:

var xhr = new XMLHttpRequest();

xhr.open("GET", "https://mytenant.sharepoint.com/site/files/picyure.png", true);

xhr.responseType = "arraybuffer";

xhr.onload = function (event) {
  
  var imageAsArrayBuffer = xhr.response;
  console.log(imageAsArrayBuffer);
  setTimeout(function() {
        console.log(imageAsArrayBuffer);
  }, 2000);    
  if (imageAsArrayBuffer) {
    $.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");
            }
        });
  }
};

Vardhaman Deshpande: Set UserProfile Picture using Client APIs in SharePoint 2013 (vrdmn.com)

or this code (but also not working)

var getFileBuffer = function (file) {
var deferred = $.Deferred();
var reader = new FileReader();
reader.onload = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(file);
return deferred.promise();
};

var deferred = $.Deferred();

getFileBuffer($('#testFile')[0].files[0]).then(
function (arrayBuffer) {

$.ajax({
url:_spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMyProfilePicture",
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
},
success: function (data) { alert('s');
deferred.resolve(data);
},
error: function (err) { alert('e');
deferred.reject(err);
}
});

}
);

Hello @danieljr,

You can try out solutions described in this posts: