Save a Document with Metadata Programatically in Liferay


In my previous blog we see how to upload a file with metadata associated with it by using Liferay GUI.Today we will discuss how to upload a file or document with metadata in Document and Media Portlet Programmatically. Before reading this blog you must know how to create metadata sets and document type for this you can read my previous blog Save Document with Metadata in Liferay.

So lets start this step by step:-

Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a Generic portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .





Step 2:-Create Your Form
Open view.jsp and paste this code:-


Explanation:-
Here we create a simple form that contain 4 fields .Two for metadata and other two for document title and description.Form enctype is multipart/form-data because we also upload a file.

Step 3:-Create portlet.properties
Create a property file in src with name portlet.properties and paste this content:-



Step 4:-Create Controller Class
Open your java class and paste this content:-


Explanation:-

  • Here we create a method getDDMStructureByName which give us structue  in which we have to save our document.You just pass the name of metadata set to it and will return structure.
  • From line 75 to 80 we create a we create a Map of Metadata fields and pass to addFileEntry method.The "candidate_name" and "experience" is exactly same name that we are using when we create metadata sets.All other detail of this method i already explained in Folder and File Upload Programmatically .



Step 5:-Check Output
Now deploy your portlet and check output as:-
Now go to Resume folder and check uploaded file 




Project Structure:-






Hope this will Help....

You can Download Source code from Save a Document with Metadata Programmatically in Liferay.


Related Post:-