Forked from martyychang/CommunityUserPhotoUpload.component
Created
December 20, 2015 23:10
-
-
Save reuf/24165492e2c1a9b6b8cd to your computer and use it in GitHub Desktop.
Visualforce component demonstrating the use of ConnectApi to upload Chatter profile photo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:component selfClosing="true" | |
controller="CommunityUserPhotoUploadController" | |
allowDML="true"> | |
<apex:attribute name="subject" type="Id" | |
assignTo="{!userId}" | |
required="true" | |
description="The User ID for the community user"/> | |
<apex:form> | |
<apex:image value="{!largePhotoUrl}"/> | |
<apex:inputFile value="{!blobValue}" | |
contentType="{!contentType}" | |
fileName="{!filename}"/> | |
<apex:commandButton value="Upload" | |
action="{!upload}"/> | |
</apex:form> | |
</apex:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Controller for the CommunityUserPhotoUpload component | |
*/ | |
public class CommunityUserPhotoUploadController { | |
/* | |
* The binary data of the image uploaded by the user | |
*/ | |
public transient Blob blobValue { get; set; } | |
/* | |
* The content type, determined by Salesforce, of | |
* the image uploaded by the user | |
*/ | |
public transient String contentType { get; set; } | |
/* | |
* The name of the image file uploaded by the user | |
*/ | |
public transient String filename { get; set; } | |
/* | |
* The User ID of the community user for which | |
* a new profile photo is being uploaded | |
*/ | |
public Id userId { get; set; } | |
/* | |
* @return the URL for the large profile photo of the specified | |
* user in the current community | |
*/ | |
public String getLargePhotoUrl() { | |
return ConnectApi.ChatterUsers.getPhoto( | |
Network.getNetworkId(), userId).largePhotoUrl; | |
} | |
/* | |
* @return the appropriate next page after setting the | |
* specified user's profile photo to the new | |
* image uploaded | |
*/ | |
public PageReference upload() { | |
ConnectApi.BinaryInput photoFileInput = | |
new ConnectApi.BinaryInput(blobValue, contentType, filename); | |
ConnectApi.ChatterUsers.setPhoto( | |
Network.getNetworkId(), userId, photoFileInput); | |
return null; // Leave the user on the current page | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page > | |
<!-- Demo page showing how to use the | |
CommunityUserPhotoUpload component --> | |
<c:CommunityUserPhotoUpload subject="{!$User.Id}"/> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment