1/21/2011

How to move file in Liferay document library

Problem: There is no "out of the box" solution to move files in the Liferay 5.2 document library / liferay image gallery. The method got implemented with Liferay 6 so Liferay 5 users have to find another way.


Your first try might be to reset the "parentFolderId" property of the DlFileEntry you want to move and to update the file. Don´t do that ! Liferay maps the folders you store the files in the document library directly to your harddrive (if you are storing them there) and won´t find the files anymore if you manipulate the entries.


The solution is: Read the DlFileEntry and store it as a new file. Afterwards yuo can delete the old file:


// 1: Get the old content

InputStream fileAsStream = DLFileEntryLocalServiceUtil
      .getFileAsStream(entry.getCompanyId(),
       entry.getUserId(), entry.getFolderId(),
       entry.getName(), entry.getVersion());

//2: Add the new file with the old content


DLFileEntry addFileEntry = 
DLFileEntryLocalServiceUtil.addFileEntry(userid, folder1,
        entry.getName(), entry.getTitle(), entry.getDescription(),   
        null,fileAsStream, entry.getSize(), new ServiceContext());


//3: Delete the old file
DLFileEntryLocalServiceUtil.deleteDLFileEntry(entry);




But beware:


This means that you create a NEW file. A real "move " is not possible. So be sure to pass on the new fileEntryId.



If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.