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
Nidhi MalhotraNidhi Malhotra 

Custom Visualforce page to upload User's Chatter Profile Picture

Hi All,

I am trying to create a VF page to upload users's chatter profile picture. I know about the OOB way but we have a requirement to upload it using Visual force pages. 

Following is the code that I have developed. But I am recieving the following error. Please help me to troubleshoot this or if there is any workaround. I am okay to use any other fuction, but once the user uploads an image that has to be set as her profile. Even if that is cropped automatically and saved that is fine, or if we can even crop it that also will be great. 

I recieve following error: ConnectApi.ConnectApiException: Illegal value for parameter: 'fileId': 015L0000000ENip
Error is in expression '{!upload}' in component <apex:commandButton> in page profilephotoupload

An unexpected error has occurred. Your solution provider has been notified. (ConnectApi)

Following is the code. 

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>

Controller:

public without sharing class FileUploadController {

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

  public PageReference upload() {
    Id ImageId;
    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 = document.id;
      //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 : '+ImageId ));
    String communityId = null;
    String userId= UserInfo.getUserId();
    ID fileId = ImageId;

    // Set photo
    ConnectApi.Photo photo = ConnectApi.ChatterUsers.setPhoto(communityId, userId, fileId, null);
    return null;
  }

}

Thanks a lot,
Nidhi M
Best Answer chosen by Nidhi Malhotra
Pavan Kumar KajaPavan Kumar Kaja
Hi Nidhi,

Chabge your controller like below. Let me know if any questions.


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;
  }

}


All Answers

Pavan Kumar KajaPavan Kumar Kaja
Hi Nidhi,

Chabge your controller like below. Let me know if any questions.


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;
  }

}


This was selected as the best answer
Pavan Kumar KajaPavan Kumar Kaja
Hi Raghavendra,

Please mark it as solved. So that it can be usefull for others.


Thanks,
Pavan Kumar