0

Create link for a document library in a portlet

  • Asked - Oct 12, 2011, 07:01 AM
    / Viewed 64773 Times
    Hi emoticon,

    I'm creating a portlet where users will be able to download files from the liferay document library. I'm able to retrieve and display alldocuments from the database (with com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil) but I want to display a link to download the file, how to do it ? any ideas ?emoticon

    Regards,
    Romain.

    1 Answers

    0
    Answered - Oct 12, 2011, 08:26 AM
    (Edited)
    Hi Romain,

    why not using the already existing document library display portlet? If you want to have your own portlet you can refer to the dl display portlet on how to create the download links.

    Greets Oli
    Replied - Oct 12, 2011, 12:38 PM
    (Edited)
    Hi Olivier,

    Ok I think this is a good idea, but where could I find the default liferay document portlet? How to see the source code??

    Thanks,
    Romain.
    Replied - Oct 14, 2011, 08:27 AM
    (Edited)
    Hi Romain,

    sorry for the late reply.

    You can find the document library portlet in the control panel or you can add the "document library display porlet" onto each page you like by using the "add application" menu. If you're using Liferay trunk or nightly builds I think it should be called "documents and media portlet" or sth that way (if I remember right).

    The source code from the jsps can be found at "webapps \ ROOT \ html \ portlet \ document_library*". If you need the backend/ bean code download Liferay src and look out for DL* interfaces and classes like e.g. "DLFolder" which defines a folder or "DLFileEntry" which defines any element uploaded to the document library . If you want to add/ modify/ delete a file/ folder from the document library take a look at the DL*LocalServiceUtil classes e.g. DLFolderLocalServiceUtil.

    HTH Oli
    Replied - Oct 20, 2011, 06:46 AM
    (Edited)
    Thank you so much Olivier (and sorry for the delay..)

    I found all files and the document portlet and I managed to find the link to download a file ! emoticon It possible to use it in an other portlet like that :
    Java code
    
    DLFileEntry fileEntry =com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil.getFileEntry(fileId);
    fileEntry = fileEntry.toEscapedModel();
    
    long fileEntryId = fileEntry.getFileEntryId();
    long folderId = fileEntry.getFolderId();
    String name = fileEntry.getName();
    String extension = fileEntry.getExtension();
    String title = fileEntry.getTitle();
    
    ThemeDisplay themeDisplay=null;
    if (renderRequest != null) {
      themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
    }
    String fileUrl = themeDisplay.getPortalURL() + themeDisplay.getPathContext() + "/documents/" + themeDisplay.getScopeGroupId() + "//" + folderId +  "//" + HttpUtil.encodeURL(HtmlUtil.unescape(title));
    


    HTML code
    <a href="<%=fileUrl %>">Download the file</a>


    Thank olivier!!
    Replied - Mar 7, 2013, 12:07 PM
    (Edited)
    It is fine we can create document link as suggested above.
    But when we do not have themdisplay object how can we create that link?
    What I have is just fileentry object inside my model listener?
    Anyone has any idea?
    Replied - Aug 8, 2013, 01:31 PM
    (Edited)
    Dear Romain NIO,

    Create link for file works perfect .Can we create HyperLink for folder ???




    Thanks & Regards
    Manish Banwari lal yadav
    Replied - Aug 23, 2013, 09:36 AM
    (Edited)
    DLFileEntry fileEntry =com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil.getFileEntry(fileId);

    Can Any one Suggest me where we get it fileId value?

    When I am using this code then give the following error:
    fileId cannot be resolved to a variable...

    Thanks,
    Pawan
    Replied - Aug 26, 2013, 11:56 AM
    (Edited)
    Hi Pawan,

    I think it is fileEntryId.

    Please use codes mentioned below, as an example to get fileEntryId.

    First Create a long variable fileEntryId .


    List<DLFileEntry> ls = com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil.getDLFileEntries(-1,-1);

    for(DLFileEntry ts:ls)
    {

    fileEntryId = ts.getFileEntryId();

    }

    Thanks,