Search This Blog

Tuesday 17 December 2013

Dynamically Creating a URL for a DLFileEntry or a File stored in Document & Media

Recently, i came across a requirement where i need to provide a view link to the various files stored in Liferay Document & Media.ie on click of these links the File(.pdf stored in our case) needs to open in a new Tab in Browser.
Hence, a url is required for that file :
I came to know that the complete URL formed is composed of the following components:
host+documents+groupId+folderId+fileTitle
where,
host - you can obtain as following- 'PortalUtil.getPortalURL(portletRequest)'
-will return something like this (http://localhost:8080/) incase of local machine..
documents - you can hardcode as this is hardly going to change in the D&M path..
groupId - you better fetch from the dlFileEntry Obj as - 'dlFileEntry.getGroupId()'
folderId - you better fetch from the dlFileEntry Obj as - 'dlFileEntry.getFolderId()'
fileTitle - you better fetch from the dlFileEntry Obj as - 'dlFileEntry.getTitle()'
- Note: Don't fetch 'dlFileEntry.getName()' instead of 'dlFileEntry.getTitle()'

Hence,
The complete url will look something like this:
 'http://localhost:8080/documents/10179/0/Flipkart-Induct-meet'

Here's how i formed this URl in my PortletClass:
------------------------------------------------------------------------------------------------------------
final String filePath_fromDM = "documents/"+dlFileEntry.getGroupId()+"/"+dlFileEntry.getFolderId()+"/"+dlFileEntry.getTitle();
System.out.println(">>>>>>  filePath_fromDM >>> = "+filePath_fromDM);
-will return something like this (documents/10179/0/Flipkart-Induct-meet) incase of local machine..

String completeFilePathUrl = StringPool.BLANK;
completeFilePathUrl = PortalUtil.getPortalURL(resourceRequest)+"/"+filePath_fromDM;
System.out.println(" >>> completeFilePathUrl () = "+completeFilePathUrl );
-will return something like this (http://localhost:8080/documents/10179/0/Flipkart-Induct-meet) incase of local machine..
------------------------------------------------------------------------------------------------------------

And, finally here how i formed a Link for this URL on click of which it will open the doc in a New-Tab..
String linkFormed = "<a href='"+scopeUrl+"' target='_blank'>View</a>" ;
Note: target="_Blank" is required only if you want to open the link in a new Browser Tab.
which you can pass in a response , use in a jsp , etc as per your requirement..


No comments:

Post a Comment