function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anjsjainanjsjain 

Update user profile photo - snallPhotoUrl

Hello ,

 

I have a requrement wherein a Blob field in a custom object (storing the user photo) needs to be displayed as chatter user profile photo.

Currently , the User smallphotourl field is used to display the chatter user profile photo. How can this be updated to use my custom photo?

 

Its a critical requirement. Response is appreciated!

 

Thanks

anjs

 

Best Answer chosen by Admin (Salesforce Developers) 
alouiealouie

Hi Anjs,

 

You're very close! I think the problem is in here:

 

           Part[] parts = {
                new StringPart("fileName", "My Image"),
                new FilePart("feedItemFileUpload", contentFile)        };

 

You only need to have a FilePart, and according to the documentation, the parameter name needs to be "fileUpload". Try replacing the above with this:

 

           Part[] parts = {
                new FilePart("fileUpload", contentFile)        };

All Answers

anjsjainanjsjain

Can anyone please help me out in this. I am not getting any reference of this.  Is there any  way by which I can update the chatter user profile photo?

 

alouiealouie

Hi anjsjain,

 

The Chatter UI in the Salesforce CRM app will only display the user profile photo contained in the smallPhotoUrl and largePhotoUrl fields. You'll need to take the blob and convert it into an image file -- then you can update the context user's profile photo with the REST API by POSTing to

 

/chatter/users/me/photo

 

This is documented in the User Photos section of http://www.salesforce.com/us/developer/docs/chatterapi/Content/connect_resources_users.htm

 

Hope that's an acceptable solution?

 

cheers!

anjsjainanjsjain

Hi Alouie,

Thanks for the info. I dont have much idea on REST API , so in case you have any sample example then it would be really helpful.

thanks

Anjs

 

 

 

alouiealouie

Hi Anjs,

 

If you're new to the Chatter REST API, there are some tips for getting started here:

 

http://boards.developerforce.com/t5/Chatter-and-Chatter-API/How-to-start-learning-chatter-REST-api/td-p/371397

 

and the documentation is quite good:

 

http://www.salesforce.com/us/developer/docs/chatterapi/index.htm

 

Once you have the authentication part working, try POSTing to the users resource as I described above. Happy to help if you run into any problems.

 

Good luck!

anjsjainanjsjain

Hi Alouie,

 

Can we use Rest API from within a trigger/apex class ? If yes, how can that be done.

I have a custom object with a blob field that contains the user photo.The API within a trigger can be used to post this photo to the user smallPhotoUrl/largePhotoUrl fields?

 

Pls suggest!

 

thanks

Anjs

 

 

alouiealouie

Hi Anjs, sorry, I didn't realize that you wanted to do this from a trigger. Unfortunately, the REST API commands can't be called from Apex yet.

anjsjainanjsjain

So if it needs to be done from apex code/trigger then do we have any way to do it? Is it possible that I save the photo data as a file in Salesfurce (Content, Document, as an attachment, etc) and then update the smallphotourl field by the location of the file? Any suggestions? 

anjsjainanjsjain

Hi Alouie,

I have written a java class that fetches the user profile photo successfully. Hence OAuth authentication is working fine. However when I am posting a image , it is giving erorr

 returnCode 500
[ {
  "message" : "attachment error",
  "errorCode" : "UNKNOWN_EXCEPTION"
} ]

Do you have any idea what could be the issue?

 

Code snippet that i am using to upload the image :

 

String urlString = "https://login.salesforce.com/services/data/v23.0/chatter/users/me/photo";

            PostMethod postMethod = new PostMethod(urlString);

           File contentFile = new File("c:/img/model.png");

           Part[] parts = {
                new StringPart("fileName", "My Image"),
                new FilePart("feedItemFileUpload", contentFile)        };
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            postMethod.setRequestHeader("Authorization", "OAuth " + oauthToken);
            postMethod.addRequestHeader("X-PrettyPrint", "1");
            HttpClient httpClient = new HttpClient();
            //httpClient.getParams().setSoTimeout(60000);
            System.out.println("Uploading " + fileName + " to " + urlString);
            int returnCode = httpClient.executeMethod(postMethod);
            System.out.println(" returnCode "+ returnCode);
            System.out.println(postMethod.getResponseBodyAsString());

 

Please help me out in case I am doing anything wrong here.

 

Thanks

Anjs

alouiealouie

Hi Anjs,

 

You're very close! I think the problem is in here:

 

           Part[] parts = {
                new StringPart("fileName", "My Image"),
                new FilePart("feedItemFileUpload", contentFile)        };

 

You only need to have a FilePart, and according to the documentation, the parameter name needs to be "fileUpload". Try replacing the above with this:

 

           Part[] parts = {
                new FilePart("fileUpload", contentFile)        };

This was selected as the best answer
anjsjainanjsjain

Hi Alouie,

 

Yeah you are right .Thanks for your time! After posting the query to you, I read the documentation and found that it just needs either fileUpload or fileId as req parameter. I changed that and it worked as a charm!!

Thanks again for taking time and helping me out !!

 

Posting the final code snippets, might help others:-)

String urlString = "https://<salesforce instance>/services/data/v23.0/chatter/users/me/photo";

 PostMethod postMethod = new PostMethod(urlString);

Part[] parts = {
                new FilePart("fileUpload", contentFile)        };
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            postMethod.setRequestHeader("Authorization", "OAuth " + oauthToken);

            postMethod.addRequestHeader("X-PrettyPrint", "1");
            HttpClient httpClient = new HttpClient();
            httpClient.getParams().setSoTimeout(60000);
            System.out.println("Uploading " + fileName + " to " + urlString);
            int returnCode = httpClient.executeMethod(postMethod);
            System.out.println(postMethod.getResponseBodyAsString());

 

Thanks

Anjs

Sandeep AgrawalSandeep Agrawal

Can you please tell me how to post the image to user wall? I am trying it but not able to succeed.

Thanks