Pages

Thursday, May 31, 2018

Salesforce: Files sharing Query

More than 3 years ago, I wrote a blog Content Architecture sharing about how to query and the content still valid till now. This blog would be the continuation on how to query the File is sharing to who or what, and the level of visibility.

There is an object called ContentDocumentLink (with prefix 06A), this object is a child of ContentDocument (prefix 069). This object 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.

To query this object, it must be filtered on one of Id, or ContentDocumentId, or LinkedEntityId. Let's see some samples:

SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink 
This query will throw an error because of not filter on anything.
MALFORMED_QUERY: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.


To query for a file is sharing to
SELECT Id, ContentDocumentId, ContentDocument.Title, LinkedEntityId, LinkedEntity.Name, LinkedEntity.Type, ShareType, Visibility FROM ContentDocumentLink WHERE ContentDocumentId = '0690k000000TzTMAA0'



To query all files shared to a record
SELECT Id, ContentDocumentId, ContentDocument.Title, LinkedEntityId, LinkedEntity.Name, LinkedEntity.Type, ShareType, Visibility FROM ContentDocumentLink WHERE LinkedEntityId = '0010k00000IKS3YAAX'



To query all files shared to an object
SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId in (SELECT Id FROM Account)




ReferenceContentDocumentLink



3 comments:

  1. Hi Johan,

    This is an old post that I came across today. Do you know of a way to query all files that would show their visibility? When I query ContentDocument, I can query all files using the new permission 'QueryAllFiles', but Visibility isn't an option in dataloader for ContentDocument. It is in ContentDocumentLink, but in that case, OwnerId isn't an option. Basically what I'm trying to do is query all files owned by a certain user, but show me the sharing visibility.

    ReplyDelete
  2. Hi Johan,

    I am trying to migrate all the data from my legacy system to salesforce. I have to migrate notes . now i am facing issues in migration, could you please help ?

    I want to link notes with my custom object data. and the linking entry is going to be in ContentDocumentLink and post is not available on this object.

    Could you please help me in this

    ReplyDelete

Page-level ad