2

I'm trying to use ConnectApi.ChatterUsers.getUserBatch() to migrate users' photos. However, the ConnectApi.User object that I get only seems to have access, through its photo property, to the ConnectApi.Photo.photoVersionId value. However, when using ConnectApi.ChatterUsers.setPhoto() I need to actually provide a fileId, not a photoVersionId.

My question is: Knowing the photoVersionId, how do I get the fileId?

Below is a sample anonymous Apex code block that illustrates what I'm trying to do. It will reproduce the same error I've described above.

Id fromUserId = '005j0000000Z1Lx';
Id toUserId = '005j0000000Zqt3';

for (ConnectApi.BatchResult eachResult :
    ConnectApi.ChatterUsers.getUserBatch(
        null, new List<Id> { fromUserId })) {
    ConnectApi.User eachUser = (ConnectApi.User)eachResult.getResult();
    ConnectApi.ChatterUsers.setPhoto(null, toUserId, eachUser.photo.photoVersionId, null);
}
2

BritishBoy above is correct -- so the only way I could find to access the User Photos is through the URL:

Id fromUserId = '005o0000001drai';
Id toUserId = '005o0000000ygsZ';

for (ConnectApi.BatchResult eachResult :
     ConnectApi.ChatterUsers.getUserBatch(
         null, new List<Id> { fromUserId })) {
             ConnectApi.User eachUser = (ConnectApi.User)eachResult.getResult();
             ConnectApi.Photo p = ConnectApi.ChatterUsers.getPhoto(null, fromUserId);
             Http h = new Http();
             HttpRequest req = new HttpRequest();
             req.setEndPoint(p.fullEmailPhotoUrl);
             req.setMethod('GET');
             HttpResponse res = new Http().send(req);
             Blob b = res.getBodyAsBlob();
             ConnectApi.ChatterUsers.setPhoto(null, toUserId, 
                 new ConnectApi.BinaryInput(b, res.getHeader('Content-Type'),'user3.jpg'));
         }

Note that there's a couple caveats with the method above:

  1. You need to authorize your Salesforce instance as a "Remote Site"
  2. I was only able to get this to work with the fullEmailPhotoUrl property, as it doesn't need authentication
1
  • For Group Photos, all you would need to do is replace "ChatterUsers" with "ChatterGroups"! – Julien Ouellet Nov 13 '14 at 20:34
4

I saw this in the docs re PhotoInput, so I am thinking it can't be done?

Images uploaded on the Group page and on the User page don’t have file IDs and therefore can’t be used.

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.