Accessing Chatter user pics

SNAG_Program-0012 Anyone doing Chatter development will want to display a user's Chatter profile picture (probably the thumbnail version) at some point. For example, if you're recreating a Chatter feed, you'll want to display the author's thumbnail pic next to the relevant post/comment, just like in the standard Chatter application.

Starting in Winter '11, you can access the URL for a user's Chatter profile picture via the 'FullPhotoUrl' and 'SmallPhotoUrl' fields on the User object. If you wanted to display either of these pictures from within the context of an existing Salesforce browser session (e.g. from a Visualforce page), then simply adding a link to either of those 2 URL's would do the trick. However, what if you wanted to display the user profile picture in an application/device where no Salesforce browser session exists. Say for example you're developing an Android/iOS application that needs to display a user's Chatter profile picture. Simply linking to the 'FullPhotoUrl' or 'SmallPhotoUrl' URL would not work – the standard Salesforce login page would get displayed. Chatter profile pictures are stored internally in Salesforce CRM Content and as you would expect, you need a valid user session in order to access any data stored in Content (i.e. it is not available publicly without user authentication). 

Here's a neat solution to the above conundrum. Simply append a valid Salesforce session Id or OAuth access token to the 'FullPhotoUrl' or 'SmallPhotoUrl' URL as a query parameter – i.e. add '?oauth_token=<insert valid session id/OAuth token>' to the end of the 'FullPhotoUrl'/'SmallPhotoUrl' value. The next question is of course how to get a valid session id or OAuth access token. Depending on the use case, you can either use the SOAP API (the login call specifically) to acquire a valid session Id or implement the OAuth 2.0 protocol to acquire an access token. 

Note that even if you use a session id, the name of the parameter remains 'oauth_token'. Note also that appending an OAuth token or session id to the end of the URL only works for Chatter user pics that are stored in CRM Content – you cannot access any other data stored in CRM Content this way.

Try it out and remember to upload a picture to your Chatter profile! You don't want to be the 'He-Who-Has-No-Picture' guy.

tagged Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • A. Van Someren

    Good thing you are making this public.
    Though, there is one issue i am not able to solve while using the soap api.
    In some organisations the smallphotourl is not accessible via createdby.profile.createdby relation.
    Amy ideas on why this coups be happening?

    • Sandeep Rachha

      hi sandeep
      iam working an iphone app with salesforce integration.
      This article is excellent.what i need.i spent total 3 days to display user profile pic.this article really helped me alot.
      how can i update my profile pic.
      how can i use rest api to update profile pic.To what object profile pic belongs to? so that i can write update query on that coloumn of that object.

      Thanks,
      Sandeep kumar

  • A. van Someren

    Last sentence edit (damn i-phones with auto correction…)
    Any ideas on why this could be happening?

  • Hmmm… That’s interesting. Where did you first hear about this? Do you have other blog posts I can take a look at?Monica Cerna RodriguezMK Partners IncSalesforce Consulting Experts

  • Funny image Sandeep 🙂

  • Awesome post… been looking for something like this since before FullPhotoURL and SmallPhotoURL were part of the API. I was on the write track, I tried it with “?sid=” with no luck but this works perfectly. Thanks!!

  • Mark Waddle

    Do not use CreatedBy.Profile.CreatedBy. I have seen a post on the boards that recommends this, but it is not what you want. That gives you the user who created the poster’s profile. To get the post creator use FeedPost.CreatedBy. This is a User object. To get their photo use FeedPost.CreatedBy.SmallPhotoUrl.

  • Anjali Jain

    Nice post ! Well is it possible to update the user profile photo of the users ? I have a custom object that stores the photo data of the users as a blob. I need to update the user profile photo of all the users with this photo data.Can this be done from within apex code? If yes, can you please provide some pointers.

    • Anonymous

      Anjali – in order to update a user’s picture in Chatter, you’ll have to use the Chatter REST API. You can’t unfortunately do it directly via Apex.
      Look at the ‘User Photo’ section of this API documentation page to understand how you can send a POST request via the Chatter REST API to update a user’s picture – http://www.salesforce.com/us/developer/docs/chatterapi/index_Left.htm#CSHID=connect_resources_users.htm|StartTopic=Content%2Fconnect_resources_users.htm|SkinName=webhelp. You can try making a Chatter REST API callout via Apex and see if that works. Haven’t tried it myself, but should work in theory. Hope this helps.

    • Anonymous

      Anjali – in order to update a user’s picture in Chatter, you’ll have to use the Chatter REST API. You can’t unfortunately do it directly via Apex.
      Look at the ‘User Photo’ section of this API documentation page to understand how you can send a POST request via the Chatter REST API to update a user’s picture – http://www.salesforce.com/us/developer/docs/chatterapi/index_Left.htm#CSHID=connect_resources_users.htm|StartTopic=Content%2Fconnect_resources_users.htm|SkinName=webhelp. You can try making a Chatter REST API callout via Apex and see if that works. Haven’t tried it myself, but should work in theory. Hope this helps.

      • Ben Lorenz

        check out http://www.cloudspokes.com. There were a couple contests on there relating to auditing and updating/uploading chatter user photos.

  • Any ideas on how to expose chatter profile images through a Salesforce Site?

    • Saurabh

      In the site page controller log in as an admin user using web services to generate session Id and use it in token variable.

      Check sample code @ https://gist.github.com/3874483

      • Thanks for the suggestion, but I don’t want to embed credentials in apex, and I definitely don’t want to expose valid session tokens for an admin user to an unauthenticated Site user.

  • Isn’t this a hole? What would stop a knowledgeable user from taking the session id and using it to access other information via the api?

  • Hi,
    I wanted to show some sfdc user’s profile pics inside customer portal. I tried accessing FullphotoURL with several ways including the above one with session id, but server responds back with ‘404’.
    I understand that chatter is not opened yet for customer portal users. However is there any work around to get the profile pics of users?

    Although, nice post. It helped me several times in last 3months.
    Thanks,
    Bhupendra.

  • Edgar Yucel Moran

    This url is generated when I click on the Chatter user profile user (https://c.na7.content.force.com/profilephoto/0000000V8QX/F). I wonder what kind of Id is that. Maybe with this id and the session started we are able to see the profile picture.

  • Nikita Karpenkov

    There is better solution using chatter connect API.

    public String getUserPhotoURL(String UserId) {
    ConnectApi.Photo ph = ConnectApi.ChatterUsers.getPhoto(null, UserId);
    return ph.fullEmailPhotoUrl;
    }

    Read more in my blog:
    http://sforcenotes.blogspot.com/2015/10/solution-to-display-salesforce-user.html