Pages

Thursday, May 31, 2018

Salesforce: Files sharing Query

More than 3 years ago, I wrote a blog Content Architecture sharing how to query. 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 version 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 filtering 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, ContentDocument.Title, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Account)


To query all files shared
SELECT Id, ContentDocumentId, ContentDocument.Title, LinkedEntityId, LinkedEntity.Name, LinkedEntity.Type, ShareType, Visibility FROM ContentDocumentLink WHERE ContentDocumentId in (SELECT ID from ContentDocument)





Reference

4 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
  3. Hi Johan,

    I am using below SOQl to get sharing details of a particular file, With below query I am able to get sharing details if file is shared to a particular User, Group and Organization but Its not giving sharing details if file is shared with Public URL, for that I have to Query ContentDistribution table separately and that too I am not sure while querying that file is shared with Public URL or not.

    SELECT Id, ContentDocumentId, LinkedEntityId, LinkedEntity.Name, LinkedEntity.Email, LinkedEntity.Type, ShareType, Visibility FROM ContentDocumentLink WHERE ContentDocumentId='XXXXXXXXXXXXXXXXXX'

    I am looking for a way where I can get all sharing details of a file including Public URL sharing details with a single SOQL query. Any leads to solve my use case will be much appreciable. Thank You.

    ReplyDelete

Page-level ad