Supported Calls
create(), delete(), describeSObjects(), query(), retrieve(), update(), upsert()
Special Access Rules
- In API versions 59.0 and later, turn on the Query All Files permission to query without a filter on id, LinkedEntityId, and documentID fields. The View All Data permission is required to turn on 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’re a member.
- For orgs with Digital Experiences enabled, a document can be shared with only 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:
SELECT 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
trigger NoShareXLSX on ContentDocumentLink (after insert) { for (ContentDocumentLink cdl : trigger.new) { if (!CDLHelper.isSharingAllowed(cdl)) { cdl.addError('Sorry, you cannot share this file.'); } } }
public class CDLHelper { /** * Gets FileExtension of the inserted content. */ public static String getFileExtension(ContentDocumentLink cdl) { String fileExtension; String docId = cdl.ContentDocumentId; FileExtension = [select FileExtension from ContentVersion where ContentDocumentId = :docId].get(0).FileExtension; return FileExtension; } /** * Checks the file's PublishStatus and FileExtension to decide whether user can share the file with others. * PublishStatus 'P' means the document is in a public library. */ public static boolean isSharingAllowed(ContentDocumentLink cdl) { String docId = cdl.ContentDocumentId; ContentVersion version = [select PublishStatus,FileExtension from ContentVersion where ContentDocumentId = :docId].get(0); if (version.PublishStatus.equals('P') && (version.FileExtension != null && version.FileExtension.equals('xlsx'))) { return false; } return true; } /** * Gets the parent account name if the file is linked to an account. */ public static String getAccountName(ContentDocumentLink cdl) { String name; String id = cdl.LinkedEntityId; if (id.substring(0,3) == '001') { name = [select Name from Account where Id = :id].get(0).Name; } return name; } }
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.