11

We're trying to build a custom Visualforce related list that will show all the Chatter Files that are related to a record. All files should be displayed, regardless of if they are explicitly shared with the user viewing the record. By having access to the record, the users automatically see all Chatter Files attached to that record.

Having some trouble finding the right tables to get that data back in the right way. Anyone done this before?

1
  • Removed apex and visualforce tags since this question doesn't really go there.
    – metadaddy
    Aug 17, 2012 at 15:23

4 Answers 4

17

On second though, use the LinkedEntityId field on ContentDocumentLink:

SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '[RECORD ID]'
3
  • Looping back here, this is really the way to tackle this. If you go FeedItem, you can traverse over into ContentVersion, which has big restrictions about which files you can find. By going with ContentDocumentLink, specifying the Record Id, you can easily traverse into ContentDocument, and even from there grab ContentDocument.LatestPublishedVersionId to move over to ContentVersion if you really had to. Thanks again Scott! Aug 20, 2012 at 22:04
  • 2
    Here's the query I ended up using to get access to all Chatter Files attached to a record by specifying the Record Id: SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, ContentDocument.Title, ContentDocument.CreatedById, ContentDocument.LastModifiedDate FROM ContentDocumentLink WHERE LinkedEntityId =: RecordID Aug 21, 2012 at 2:12
  • I extracted the required record ids from feeditem object and then queried in workbench for ContentDocumentLink, but how do i export the attachments. Fileexporter does seem to support the ContentDocument link object..
    – user5352
    Jun 23, 2014 at 18:09
3

I think that the best way would be querying all ContentDocument related to one record.

You have to query an object with a subquery on AttachedContentDocument:

SELECT id,  (SELECT ContentDocumentid FROM AttachedContentDocuments) FROM sObject

So you don't need to specify the linkedntityId.

2

I believe that the FeedItem object is what you are looking for. Chatter posts with a file attachment will be of type "ContentPost", so I would try this for gathering all files for a particular record id:

SELECT ContentFileName FROM FeedItem WHERE Type = 'ContentPost' AND ParentId = '[RECORD_ID]'
1
  • Thanks Scott! That got me started, and lead to question 2. In the same transaction can you grab the Ids of those actual Files? Is that a separate query, and if so, to what object? Aug 17, 2012 at 15:14
0

FeedItem is a linking object. It points to a ContentDocument record which is the file attachment and also has a parent link to the data record that the chatter post is related to. So read feeditems that have a ParentId of the data record you want, then use the associated lookup to the ContentDocument to find the ContentDocument.

Reference the Web Services API Developer's Guide. Search on FeedItem.

You must log in to answer this question.

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