How to Share a ContentDocumentLink using "Set by Record"

Set By Record can be done upon assigning

ShareType = "I", where it means:

Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library.

Other Share Types are:

V Viewer premission. The user can explicitly view but not edit the shared file.

C Collaborator permission. The user can explicitly view and edit the shared file.

Given a small piece of code for this assignment

//create an account 
Account acct = new Account(Name='TEST_ACCT');
insert acct;

ContentVersion contentVersion = new ContentVersion(
  Title = 'Test',
  PathOnClient = 'test.jpg',
  VersionData = Blob.valueOf('Test Content'),
  IsMajorVersion = true
);
insert contentVersion;    
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

//create ContentDocumentLink  record for that account
ContentDocumentLink cdl = New ContentDocumentLink(
            LinkedEntityId = acct.id, ContentDocumentId = documents[0].Id, shareType = 'I');
insert cdl;

Also, created a file with Set By Record at my org.

Chatter set by record

Workbench query

Workbench set by record

For more information, refer ContentDocumentLink


This is available now in the Winter '20 release! https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_files_sharing_set_by_record.htm