Newer Version Available
ContentDocumentLink
Supported Calls
create(), delete(), describeSObjects(), query(), retrieve(), update(), upsert()
Special Access Rules
- In API versions 59.0 and later, enable the Query All Files permission to query without a filter on id, LinkedEntityId, and documentID fields. The View All Data permission is required to enable Query All Files.
- 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. A ContentDocumentLink trigger executes whenever there is an addition or deletion of the ContentDocumentLink. When a file is deleted, a ContentDocument delete trigger executes, but the cascaded ContentDocumentLink delete does not trigger ContentDocumentLink triggers.
Example
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}Associated Objects
This object has the following associated objects. Unless noted, associated objects are available in the same API version as this object.
- ContentDocumentLinkChangeEvent (API version 55.0)
- Change events are available for the object.