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

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.
Using the API only, you can relate notes to custom settings.
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.
V
Viewer permission. The user can explicitly view but not edit the shared file.
C
Collaborator permission. The user can explicitly view and edit the shared file.
I
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.
Visibility
Type
picklist
Properties
Create, Filter, Group, Nillable, Sort, Update
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.
  • Only internal users can update Visibility.
  • Visibility can be updated on links to files posted on standard and custom object records, but not to users, groups, or content libraries.
  • Visibility is updatable in API version 43.0 and later.

The visibility setting on ContentDocumentLink 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:

  • 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.
trigger NoShareXLSX on ContentDocumentLink (after insert) {
    for (ContentDocumentLink cdl : trigger.new) {
        if (!CDLHelper.isSharingAllowed(cdl)) {
            cdl.addError('Sorry, you cannot share this file.');
        }
    }
}
The trigger calls this helper class.
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;
    }
}
Important

Important

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.

See Also