0

Background

I have created a custom upload component based on this but using ContentVersion rather than Attachment.

This custom upload component is being used on a public community page and has an Apex controller defined as:

public without sharing class FileUploadController

I am creating a ContentVersion like this:

@AuraEnabled
public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) { 

    try {

        base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');

        ContentVersion contentVersion = new ContentVersion(
            versionData = EncodingUtil.base64Decode(base64Data),
            title = fileName,
            pathOnClient =  '/' + fileName,
            FirstPublishLocationId = parentId);

        insert contentVersion; 

        return contentVersion.Id;

    } catch (Exception error) {
        throw new AuraHandledException(error.getMessage());
    }
}

And then subsequently updating the ContentVersion like this:

private static void appendToFile(Id fileId, String base64Data) {

    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');

    ContentVersion contentVersion = [
        SELECT Id, versionData
        FROM ContentVersion
        WHERE Id = :fileId
    ];

    String existingBody = EncodingUtil.base64Encode(contentVersion.versionData);

    contentVersion.versionData = EncodingUtil.base64Decode(existingBody + base64Data); 

    update contentVersion;   
}

The insert works fine, but when I try and update the ContentVersion I get this error:

Update failed. First exception on row 0 with id 0684E000000Qnt1QAC; first error: INSUFFICIENT_ACCESS_OR_READONLY, You do not have the level of access necessary to perform the operation you requested.

The community guest profile has full access to the custom object the ContentVersion.FirstPublishLocationId is being set to.

Question

  1. What is causing this error?
  2. How can I fix it?
0

It's not possible to update and ContentVersion

Therefore, to solve this, I created the upload as an Attachment and appended to the Attachment, then once the upload was completed I created the ContentVersion and deleted the Attachment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.