Newer Version Available

This content describes an older version of this product. View Latest

ContentDocumentLink

Represents the link between a Salesforce CRM Content document or Salesforce file and where it's shared. A file can be shared with other users, groups, records, and Salesforce CRM Content libraries. 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()

Special Access Rules

  • Customer and Partner Portal users must have the “View Content in Portal” permission to query content in libraries where they have access.
  • Users (including users with the “View All Data” permission) can only query files they have access to, including:
    • All Salesforce CRM Content files in libraries they're a member of and in their personal library, regardless of library permissions (API version 17.0 and later).
    • All Salesforce Files they own, posted on their profile, posted on groups they can see, and shared directly with them (API version 21.0 and later).
  • 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 LinkedEntityId 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 deleteContentDocumentLink objects between the file and organizations or groups of which they are a member.
  • For organizations with Communities enabled, a document can only be shared with users and groups that are a part of the community the file was created in.

Fields

Field Details
ContentDocumentId
Type
reference
Properties
Create, Filter, Group, Sort
Description
ID of the document.
LinkedEntityId
Type
reference
Properties
Create, Filter, Group, Sort
Description
ID of the linked object. Can include Chatter users, groups, records (any that support Chatter feed tracking including custom objects), and Salesforce CRM Content libraries.
ShareType
Type
picklist
Properties
Create, Filter, Group, Nillable, Restricted picklist, Sort, Update
Description
Required. The permission granted to the user of the shared file in a library. This is determined by the permission the user already has in the library. This field is available in API version 25.0 and later.
Viewer
The user can explicitly view but not edit the shared file.
Collaborator
The user can explicitly view and edit the shared file.
Inferred
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.
Visibility
Type
picklist
Properties
Create, Filter, Group, Nillable, Sort
Description
Specifies whether this file is available to all users, internal users, or shared users. This field is available in API version 26.0 and later.
Visibility can have the following values.
  • AllUsers—The file is available to all users who have permission to see the file.
  • InternalUsers—The file is available only to internal users who have permission to see the file.
  • SharedUsers—The file is available to all users who can see the feed to which the file is posted. SharedUsers is used only for files shared with users, and is available only when an org has private org-wide sharing on by default. The SharedUsers value is available in API version 32.0 and later.
Note the following exceptions for Visibility.
  • AllUsers & InternalUsers values apply to files posted on standard and custom object records, but not to users, groups, or content libraries.
  • For posts to a record feed, Visibility is set to InternalUsers for all internal users by default.
  • External users can set Visibility only to AllUsers.
  • On user and group posts, only internal users can set Visibility to InternalUsers.
  • For posts to a user feed, if the organization-wide default for user sharing is set to private, Visibility is set to SharedUsers.

The visibility setting onContentDocumentLink determines a file’s visibility on a record post. When a file has multiple references posted in a feed, the file’s visibility is determined by the most visible setting.

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.
A SOQL query must filter on one of Id, ContentDocumentId, or LinkedEntityId.

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.
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}
The trigger calls this helper class.
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}

Apex has a per organization limit of 10 concurrent requests that last longer than 5 seconds. A trigger that uploads files can easily hit this limit.

Important