Query ContentVersion for Files in a Specific Salesforce Library
Salesforce stores files using complex objects like ContentVersion and ContentWorkspace rather than simple attachments. You can query files in a specific Salesforce library by filtering ContentVersion records where the FirstPublishLocationId matches the library's ContentWorkspace ID. This method ensures you retrieve only the latest version of files originally published to that library, avoiding duplicates. It’s important to understand this approach only catches files first published in that library and not those shared later. For complete coverage, querying ContentDocumentLink may be necessary.
- Query ContentVersion filtering by FirstPublishLocationId for files in a specific library.
- Always include IsLatest = true to get only the most recent file versions.
- Use subquery on ContentWorkspace Name to avoid hardcoding library IDs.
- Understand FirstPublishLocationId only reflects the file's original upload location.
- Query ContentDocumentLink to find all files shared to a library regardless of origin.
How to Query ContentVersion for Files in a Specific Salesforce Library? If you have ever worked with files in Salesforce, you know that the underlying architecture is much more complex than a simple “Attachment” object. Salesforce CRM Content utilizes a highly normalized data model to handle versioning, sharing, and library storage. One of the most common challenges developers and admins face is trying to extract a list of files that live within a specific library. In this post, we will walk through exactly how to write a SOQL query to retrieve ContentVersion records tied to a specific Salesforce Library ( ContentWorkspace ). Understanding the Salesforce File Objects Before diving into the query, it is crucial to understand the two primary objects involved in this transaction. The Salesforce file architecture separates the document itself from its versions and its storage location. ContentVersion: This object represents a specific version of a document in Salesforce.