I recently wrote an apex class which queries the ContentVersion records for a lead and embeds them in a PDF page. The class is not running with Sharing, so system access, and everything is working in the sandbox.

public class ApplicationPdfController {
   for (ContentVersion c_d : [SELECT id, ContentDocumentId, Description, FirstPublishLocationId, Title FROM ContentVersion WHERE FirstPublishLocationId = :l.id]{

In the sandbox users with lead access are able to see the ContentDocuments and run the page and the ContentDocuments are embedded as expected

We just deployed the code to Production. Users with access to the leads can see the ContentDocuments. If the User that created the Lead/Files runs the PDF, the ContentDocuments are embedded as expected. But if another System Admin (who didn't create the Lead/Documents) runs the page, the query for ContentVersion isn't returning any lines, so they aren't being embedded.

In both environments only Libraries in Salesforce Files and Skip triggers execution when deploying asset files are enabled for the Salesforce Files Settings. In both environments the running user is a System Administrator. In both environments the lead and ContentDocuments were created through the same Lead Integration process, so the exact same steps were taken when creating all records. This includes the same SharingPrivacy and SharingOption values on the ContentVersion records in question

What Lightning settings or permissions would change visibility to ContentVersion records for an apex class, running in non-sharing context.

1 Answer 1


So after a lot of searching, Kris Goncalves was able to lead me in the right direction with his answer ultimately leading me to J. Schreiber answer which includes gives a query by query example

You cannot expect users, who have not explicitly been shared the File, to be able to query the ContentVersion records if they didn't also create them, even if they are an Administrator. Being able to see a Lightning files does not mean you will be able to query the ContentVersion.

Instead of trying to access through the ContentVersion, you should query the ContentDocumentLink, filtering by the LinkedEntityId

for (ContentDocumentLink c_d : [SELECT id, ShareType, Visibility, ContentDocument.Description, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId = :l.id 

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.

If you truly need the ContentVersion data, you need to filter by the ContentVersion Id. By filtering specifically by Id, you can see the rows of data you wouldn't have access to otherwise.

From the Documentation:

Note: Depending on how files are shared, queries on ContentDocument and ContentVersion without specifying an ID won't return all files a user has access to. For example, if a user only has access to a file because they have access to a record that the file is shared with, the file won't be returned in a query such as "SELECT Id FROM ContentDocument."

SELECT Id, ContentDocumentId, FirstPublishLocationId, IsLatest, ContentBodyId, VersionNumber, Title, Description, PathOnClient, OwnerId, CreatedById, CreatedDate, LastModifiedById, LastModifiedDate, FileType, PublishStatus, VersionData, ContentSize, FileExtension FROM ContentVersion WHERE id in ('068...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .