Newer Version Available
ContentDocumentLink
Represents the link between a
Salesforce CRM Content document, Salesforce file, or ContentNote and where it's shared.
This object is available in versions 21.0 and later for Salesforce CRM Content documents
and Salesforce Files.
Supported Calls
create(), delete(), describeSObjects(), query(), retrieve(), update(), upsert()
Special Access Rules
- In API versions 33.0 and later, you can create and delete ContentDocumentLink objects with a LinkedEntityId of any record type that can be tracked in the feed, even if feed tracking is disabled for that record type.
- In API versions 25.0 and later, you can create ContentDocumentLink objects with a LinkEntityId of type User, CollaborationGroup, or Organization.
- In API versions 21.0 and later, users with explicit Viewer access (the file has been directly shared with the user) to a file can delete ContentDocumentLink objects between the file and other users who have Viewer access. In the same API versions, any user with Viewer access to a file can delete ContentDocumentLink objects between the file and organizations or groups of which they are a member.
- For orgs with digital experiences enabled, a document can only be shared with users and groups that are a part of the Experience Cloud site the file was created in.
Fields
Usage
Use this object to query the locations where a file is shared or query which files are linked to a particular location. For example, the following query returns a particular document shared with a Chatter group:
1SELECT ContentDocument.title FROM ContentDocumentLink WHERE ContentDocumentId = '069D00000000so2' AND LinkedEntityId = '0D5000000089123'- You can't run a query without filters against ContentDocumentLink.
- You can't filter on ContentDocument fields if you're filtering by ContentDocumentId. You can only filter on ContentDocument fields if you're filtering by LinkedEntityId.
- You can't filter on the related object fields. For example, you can't filter on the properties of the account to which a file is linked. You can filter on the properties of the file, such as the title field.
The ContentDocumentLink object supports triggers before and after these operations: insert, update, delete.
Example
This trigger for the ContentDocumentLink object prevents public XLSX files from being
shared.
The trigger calls this helper
class.
1trigger NoShareXLSX on ContentDocumentLink (after insert) {
2 for (ContentDocumentLink cdl : trigger.new) {
3 if (!CDLHelper.isSharingAllowed(cdl)) {
4 cdl.addError('Sorry, you cannot share this file.');
5 }
6 }
7}1public class CDLHelper {
2
3 /**
4 * Gets FileExtension of the inserted content.
5 */
6 public static String getFileExtension(ContentDocumentLink cdl) {
7 String fileExtension;
8 String docId = cdl.ContentDocumentId;
9 FileExtension = [select FileExtension from ContentVersion where ContentDocumentId = :docId].get(0).FileExtension;
10 return FileExtension;
11 }
12
13 /**
14 * Checks the file's PublishStatus and FileExtension to decide whether user can share the file with others.
15 * PublishStatus 'P' means the document is in a public library.
16 */
17 public static boolean isSharingAllowed(ContentDocumentLink cdl) {
18 String docId = cdl.ContentDocumentId;
19 ContentVersion version = [select PublishStatus,FileExtension from ContentVersion where ContentDocumentId = :docId].get(0);
20 if (version.PublishStatus.equals('P') && (version.FileExtension != null && version.FileExtension.equals('xlsx'))) {
21 return false;
22 }
23 return true;
24 }
25
26 /**
27 * Gets the parent account name if the file is linked to an account.
28 */
29 public static String getAccountName(ContentDocumentLink cdl) {
30 String name;
31 String id = cdl.LinkedEntityId;
32 if (id.substring(0,3) == '001') {
33 name = [select Name from Account where Id = :id].get(0).Name;
34 }
35 return name;
36 }
37}