AnsweredAssumed Answered

Building Blocks: Removing an attachment using Content.removeContentFile()

Question asked by dahansen on Aug 5, 2015
Latest reply on Aug 10, 2015 by dahansen

Hi all,

I see that we are migrating from EduGarage to Blackboard Community. Below is a question I'd posted at EduGarage a few weeks back, but had not received a response (possibly due to the server being down recently). Thought I'd ask it again here in the hopes that someone familiar with Building Block development could assist...


As you may have seen from some of my other recent posts, I've been working on a Building Block that allows a file to be uploaded and attached to a Content item. This is now working successfully, thanks to some assistance I've received on this forum (particularly from Peter Fokkinga -- thank you!). However, I've found that after attaching a file to a Content item, then Editing that Content item to change the attached file, it will upload and attach this new file, but not remove the old one.

I saw that the Content class documentation makes mention of a removeContentFile method, so have attempted to use this to remove any existing attachments before attaching a new file. The code snippet I'm using is below:

List<ContentFile> fileList;
ContentFileDbLoader cfLoader = ContentFileDbLoader.Default.getInstance();
fileList = cfLoader.loadByContentId(contentId); // contentId is declared earlier
// There should only be one file attached, but iterate through fileList in case there are more, and remove them all
ContentFile attachedFile;

for (int i = 0; i < fileList.size(); i++) {
     attachedFile = fileList.get(i);

     out.print(attachedFile+"<BR><BR>"); // this works as expected, so I know that attachedFile is indeed a ContentFile



FYI, contentPersister.persist(contentItem); appears after this, but I've also tried placing it before, just in case that was the issue. Still no luck.

Does anyone know how to get removeContentFile() to work correctly or whether there is another way I could approach this?

Many thanks in advance for any assistance you can provide.