[SalesForce] How to upload/change user’s profile image programatically or via API

I know, that there is no way to mass-upload these images via dataloader as described here

Is there a way to access and change the image programmatically or via API? There seems to be an local app for it and I wonder on how this is done in the app: http://satrangtech.com/products.htm#4

Best Answer

As @jonathanwiesel mentioned, it can be done via Connect API which has an APEX wrapper and therefore it's easy and very straight forward to implement.

Here you can even find sample code for that: https://developer.salesforce.com/forums/?id=906F0000000AWe7IAG

I've checked it and it works fine for me like this:

VF-Page

<apex:page controller="FileUploadController">
    <apex:sectionHeader title="Visualforce Example" subtitle="File Upload Example" />

    <apex:form enctype="multipart/form-data">
        <apex:pageMessages />
        <apex:pageBlock title="Upload a File">

            <apex:pageBlockButtons>
                <apex:commandButton action="{!upload}" value="Save" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection showHeader="false" columns="2" id="block1">

                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="File Name" for="fileName" />
                    <apex:inputText value="{!document.name}" id="fileName" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="File" for="file" />
                    <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Description" for="description" />
                    <apex:inputTextarea value="{!document.description}" id="description" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Keywords" for="keywords" />
                    <apex:inputText value="{!document.keywords}" id="keywords" />
                </apex:pageBlockSectionItem>

            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>
</apex:page>

APEX-Controller

public without sharing class FileUploadController {

    public Document document {
        get {
            if (document == null)
                document = new Document();
            return document;
        }
        set;
    }

    public PageReference upload() {
        Blob b;
        document.AuthorId = UserInfo.getUserId();
        document.FolderId = UserInfo.getUserId(); // put it in running user's folder

        try {
            document.type = 'jpg';
            document.IsPublic = true;
            insert document;
            // ImageId = '06990000001HnuB';
            b = document.Body;
            //ConnectApi.ChatterUsers newPhoto = new ConnectApi.ChatterUsers();



        } catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading file'));
            return null;
        } finally {
            document.body = null; // clears the viewstate
            document = new Document();
        }

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'File uploaded successfully : ' + b));
        String communityId = null;
        String userId = UserInfo.getUserId();
        //ID fileId = ImageId;

        // Set photo
        ConnectApi.Photo photo = ConnectApi.ChatterUsers.setPhoto(communityId, userId, new ConnectApi.BinaryInput(b, 'image/jpg', 'userImage.jpg'));
        return null;
    }

}
Related Topic