As a Guest User on a Salesforce Site, executing Apex code to insert ContentDocument or ContentDocumentLink objects "silently fails".

I have configured a Site which has a simple landing page with the following 3 options available:

  1. Insert ContentDocument (with a related ContentDocumentLink to an Account)
  2. Query all ContentDocuments
  3. Query all ContentDocumentLinks related to the Account

When I preview the Site as an Admin, all 3 options work successfully.

When accessing the Site as a Guest User, the DML operation for ContentNote insert silently fails (it states in debug log that 1 DML operation completed but ContentNote cannot be located by System Administrator), while the query options always return 0 records.

The page controller is as simple as can be, see below. Does anybody know why a Guest User would not be allowed to complete such actions?

public class CreateContentDocumentController {

    public void doInsert() {
        ContentNote cn = new ContentNote();
        cn.put('Title', 'title');
        cn.put('Content', Blob.valueOf('body'));
        insert cn;

        // Account ID
        Id entityId = '0018A00000LzJob';

        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.put('LinkedEntityId', entityId);
        cdl.put('ContentDocumentId', cn.Id);
        cdl.put('ShareType', 'I');
        cdl.put('Visibility', 'AllUsers');
        insert cdl;       

    public Integer mapSize {get; set;}

    public void doQuery() {     
        // Account ID
        Id entityId = '0018A00000LzJob';

        Map<Id, ContentDocumentLink> linksMap = new Map<Id, ContentDocumentLink>([
                select Id, ContentDocument.Id, ContentDocument.FileType, LinkedEntityId
                from ContentDocumentLink
                where LinkedEntityId = :entityId]);

        mapSize = linksMap.size();   
  • A Summer '19 change may have introduced this. – TSmith Nov 25 '19 at 13:44
  • Try without sharing... – Caspar Harmer Nov 25 '19 at 21:04
  • @CasparHarmer makes no difference. This is a Salesforce issue and I will be providing a POC to their support team, which we hope will go to their Product team. – TSmith Nov 26 '19 at 13:28
  • It's likely by design, unfortunately. One technique I have implemented that works is to fire a platform event -> handle that event by performing search -> fire "results" platform event and handle that in your community. Unfortunately, this will require a custom version of the empAPI, as it is currently not supported in communties. – Caspar Harmer Nov 26 '19 at 17:10

Your Answer

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

Browse other questions tagged or ask your own question.