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?
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:
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.
Properties:- Create, Defaulted on create, Filter, Group, Restricted picklist, Sort, Update
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