Is is possible to search Salesforce Content in a specific library (ie. ContentWorkspace)?

According to the Content data model, it seems the relationship between ContentWorkspace and ContentVersion does not make it possible to filter on libraries: ie, ... ContentVersion.ContentWorkspace.Name = 'LibraryName'....

What are my options?


If you're search content specifically you'll want to use SOQL not SOSL. SOSL's big efficiency is you can search multiple fields and object in one go, but if you know what object you're searching on, just stick to SOQL. Beyond that, the documents aren't particularly clear on whether SOSL supports Content and if so which fields it's searching on.

The content data model takes a little bit of effort to get used to. You can do your query, you'll just query on the joining object and filter on the parents. If you're searching on more than one library you'll need to deal with the fact the same document could return hits from both libraries.

Here's how to build a map of matching content versions's by id for a set of library ids with titles matching a filter. The map ensures you only get one result if the matching content appears in multiple libraries. Note the use of aliases to simplify the select and where statements. Also note, you'll want the content version which contains the actual good stuff instead of the ContentDocument (usually).

Map<Id, ContentVersion> contentResults = new Map<Id, ContentVersion>();
for(ContentWorkspaceDoc workspaceDoc : [
  SELECT contentVersion.title, contentVersion.versionNumber
  FROM ContentWorkpaceDoc wd, wd.ContentWorkspace workspace, 
    wd.ContentDocument.LatestPublishedVersion contentVersion
  WHERE workspace.id in :libraryIds
   AND contentVersion.title = :titleFilter
]) {
  ContentVersion contentVersion = workspaceDoc.ContentDocument.LatestPublishedVersion;
  contentResults.put(contentVersion.id, contentVersion);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.