Find your content:

Search form

You are here

Obtaining all ContentDocument records using SOQL


According to the Salesforce documentation on ContentDocument

Users (including users with the “View All Data” permission) can only query files they have access

I find this very strange. Why would a System Administrator not be allowed to see all ContentDocument records within Salesforce?

Is there a way to circumvent this limitation?

EDIT: The weirdest thing about all this is that the Admin user can see the ContentDocument in the Salesforce web interface.. but they can't be obtained through the API. That makes little sense. I'd expect the API to show as much or more data than the web interface, not the opposite.

Attribution to: GuiSim

Possible Suggestion/Solution #1

The content data model is slightly different than other objects in salesforce.

The way it works is that each document is represented as a ContentDocument. Each ContentDocument contains one or more ContentVersion objects (depending on how many versions of a given document have been uploaded). When you are query some fields on ContentDocument, like description, you are just pulling the description from the latest ContentVersion object. To determine which ContentVersion is the latest for a given document you can look at the LatestPublishedVersionId field.

So may be thats the reason why salesforce didnt show from Web UI.

Attribution to: Arun SFDC

Possible Suggestion/Solution #2

Official response from SFDC Support is this is working as designed.

There is no ability to query all content programmatically via SOQL which I suspected. You have to query ContentDocumentLink with Filters and somehow build filter critieria across all objects in your org.

=======================CASE INFO=====================

However, this is how this functionality is designed.

As a suggestion, queries can only run under the previously mentioned conditions.

Also, the same is mentioned in the below document:

'To query a file that is shared only with a record, you must specify the content ID of the file.'

That is why ContentDocuments will be visible in the below scenarios only:

  • If the content document from a public group?
  • If the content document shared to the requesting user?
  • If the content document shared to a group that the requesting user is a member of?

Please let me know in case you have any further questions for me.

Attribution to: CoryCowgill

Possible Suggestion/Solution #3

If you want to delete files that are related to a single object then export ContentDocumentIds from below query and then use those ContentDocumentIds to delete files which are related to object.

SELECT ContentDocumentId,ContentDocument.CreatedDate,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM User)

You can replace User with your ObjectApi name.

Attribution to: sfdc

Possible Suggestion/Solution #4

Just found out today that with the Spring 19' update there is a new setting in the "Setup -> Permission Sets -> (choose an existing or create a new one) -> App Permission -> Content -> Query All Files: Allows View All Data users to SOQL query all files in the org".

This is not enabled by default for the admin user but you can at least enable it in a Profile/Permissions Set.

Also, now documented in the API Documentation.

Have fun!

Attribution to: Denis S.

Possible Suggestion/Solution #5

A bit late but this worked for me

SELECT ContentDocumentId,ContentDocument.Title, ContentDocument.CreatedDate,LinkedEntityId
FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM User)

Attribution to: Nicolas Evans

Possible Suggestion/Solution #6

As advised by David Ha, you should add a "USING SCOPE" option in your SOQL query in order to retrieve all the ContentDocument records. Strangely, I found that "USING SCOPE Everything" restricts the data to Owned files only, while "USING SCOPE Team" gives all ContentDocument regardless of Owner and sharing.

So this query did the trick for me (on API version > 32) :

Select Id, Title, FileExtension, CreatedDate From ContentDocument USING SCOPE Team

Attribution to: Sylvain

Possible Suggestion/Solution #7

We had a similar issue with Report and it turned out the reports were under the Private space of the owners. In our case, this query gives us the full data:

Select id,name,ownerid from report USING SCOPE allPrivate

Attribution to: David Ha
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content