[SalesForce] List of an Account’s Chatter files in a Visualforce page

An easy to see list of all files added to an Account (or other object) would be nice.
I am getting a "Content cannot be displayed: Attempt to de-reference a null object" for my VF page. The Account I am testing on does have a loaded file.
Class:

public class accountFileExt {

Account a;

public accountFileExt (ApexPages.StandardController controller) {
    a = (Account) controller.getRecord();        
}

public List <ContentDocument> getAccountsContentDocuments () {
    Map<Id, ContentDocumentLink> m = new Map<Id, ContentDocumentLink>([
            select ContentDocumentId
            from ContentDocumentLink
            where LinkedEntityId =: a.id
            ]);
    return [
            select Title, OwnerId, ParentId, PublishStatus
            from ContentDocument
            where Id in :m.keySet()
            order by Title
            ];
}

}

I have updated the query class but am not sure why it gets 0 results for second query.

and here is the page template

<apex:page standardController="Account" extensions="accountFileExt">
    <apex:pageBlock title="Account Files">
        <apex:pageBlockTable value="{!AccountsContentDocuments}" var="c">
            <apex:column value="{!c.title}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Thanks

Best Answer

This class worked for me - I think the way you are building the map is causing the issue. You are using the standard Map constructor, but I think the map key needs to be the ContentDocumentId field, not the Id field - once I changed that, it seems to work

public class accountFileExt{

Account a;

public accountFileExt (ApexPages.StandardController controller) {
    a = (Account) controller.getRecord();        
}

public List <ContentDocument> getAccountsContentDocuments () {
    Map<Id, ContentDocumentLink> m = new Map<Id, ContentDocumentLink>();
    for (ContentDocumentLink cl: [Select ContentDocumentId
            from ContentDocumentLink
            where LinkedEntityId =: a.id
            ]){
            m.put(cl.ContentDocumentId, cl);
            }
    return [
            select Title, OwnerId, ParentId, PublishStatus
            from ContentDocument
            where Id in :m.keySet()
            order by Title
            ];
}
}
Related Topic