вторник, 13 октября 2015 г.

Solution to display Salesforce user profile photos on external Force.com sites without authentication

Displaying Salesforce user profile images on external sites is a wide known problem. Access to User.SmallPhotoURL or User.LargePhotoURL needs authentication and that's the trouble. There are various solutions provided by different Salesforce developers, for example:
  • authorizing via OAuth by attaching "?oauth=[your-token]" to photo url - seriously?.. send valid access token to unauthorized client?
  • saving user photos as custom objects attachments - pretty tricky to implement and really ugly solution.
But there is a simple and 100%-working solution (thanks to Denis from Synebo)!

public String getUserPhotoURL(String UserId) {
    ConnectApi.Photo ph =  ConnectApi.ChatterUsers.getPhoto(null, UserId);
    return ph.fullEmailPhotoUrl;
}
 Notice, that resulting URL expires 30 days after it was obtained. It doesn't matter if you are using it on dynamic pages (since you are requesting a new URL every time when page is reloaded), but keep it in mind when using URL in some static stuff (emails, static pages, etc).

Комментариев нет:

Отправить комментарий