[SalesForce] How to get Chatter photo fileId

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);
}

Best Answer

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
Related Topic