Created
January 24, 2024 19:49
-
-
Save svierk/01b4f221e3f554bf2216b6b8520311d6 to your computer and use it in GitHub Desktop.
Apex Class for Content Document Table Component | getDocuments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AuraEnabled(cacheable=true) | |
public static String getDocuments(String library, String folder, Id recordId) { | |
try { | |
String folderName = folder != null ? folder : recordId.getSObjectType().getDescribe().getName(); | |
// get workspace library | |
ContentWorkspace cw = [ | |
SELECT Id, Name | |
FROM ContentWorkspace | |
WHERE Name LIKE :library | |
WITH USER_MODE | |
LIMIT 1 | |
]; | |
// get specific folder from library based on object api name | |
ContentFolder cf = [ | |
SELECT Id, Name | |
FROM ContentFolder | |
WHERE ParentContentFolder.Name = :cw.Id AND Name = :folderName | |
WITH USER_MODE | |
LIMIT 1 | |
]; | |
// get all files that are in the folder | |
List<Id> cfmIdList = new List<Id>(); | |
for (ContentFolderMember cfm : [ | |
SELECT Id, ParentContentFolderId, ParentContentFolder.Name, ChildRecordId, ChildRecord.title | |
FROM ContentFolderMember | |
WHERE ParentContentFolderId = :cf.Id | |
WITH USER_MODE | |
]) { | |
cfmIdList.add(cfm.ChildRecordId); | |
} | |
return JSON.serialize( | |
[ | |
SELECT Id, Title, Description, FileExtension, FileType, ContentSize, CreatedDate | |
FROM ContentDocument | |
WHERE Id IN :cfmIdList | |
] | |
); | |
} catch (Exception e) { | |
System.debug('The following exception has occurred: ' + e.getMessage()); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment