0

My requirement is to make a Salesforce file private using Apex. I tried to update the PublishStatus field (on ContentDocument), but this field is not writable. Any idea?

0

In order to make a File "Private", you should use SharingPrivacy field instead of PublishStatus. "SharingPrivacy" is a picklist type field having two values 'N' & 'P'. Setting this field to "P" makes the field Private.

ContentDocument cd = [ Select Id, PublishStatus, SharingOption, SharingPrivacy from ContentDocument where Id = '0690o000006uMZkAAM' ];
cd.SharingPrivacy = 'P';

Supporting documentation on this:

Make a File Private on a Record

Make a File Private

0

Instead of using ContentDocument to make it private, Use ContentVersion Object. You can not insert ContentDocument. Whenever you are inserting the ContentVersion, a contentDocument is getting created by default in the background.

Use SharingPrivacy field available on ContentVersion.

Type:- picklist

Properties:- Create, Defaulted on create, Filter, Group, Restricted picklist, Sort, Update

Description

Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others. This field is available in API versions 41.0 and later.

You can eliminate one soql query and one dml statement in your transaction by using ContentVersion.

Read more here:- ContentVersion

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.