Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mhamzas/1d6b4c26f39676fe4867e7425edac702 to your computer and use it in GitHub Desktop.
Save mhamzas/1d6b4c26f39676fe4867e7425edac702 to your computer and use it in GitHub Desktop.
Find All Content Version Ids from Library and Generate File download URLs
string LibraryName='Demo Download Library';
ContentWorkspace ws = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE Name = :LibraryName LIMIT 1];
Set<string> fileNameList = new Set<string>();
List<ContentDocumentLink> filesToDelete= new List<ContentDocumentLink>();
string csvString='';
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
for(ContentDocumentLink con:[select id,LinkedEntityId,ContentDocumentId ,ContentDocument.LatestPublishedVersion.Title from ContentDocumentLink where LinkedEntityId=:ws.Id]){
csvString = csvString +domainUrl+'/sfc/servlet.shepherd/version/download/'+con.ContentDocument.LatestPublishedVersionId+'\n';
}
string instanceURL= URL.getSalesforceBaseUrl().gethost();
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(csvString);
string csvname= instanceURL +':'+LibraryName+'-Files download URL.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {UserInfo.getUserEmail()};
mail.setToAddresses(toAddresses);
mail.setSubject(instanceURL +':'+LibraryName+'-Files download URLs');
mail.setPlainTextBody(LibraryName+'-Files download URLs. Please refer the attachment.');
mail.setHtmlBody(LibraryName+'-Files download URLs. Please refer the attachment.');
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment