How to programatically add images and documents to Liferay

If you want to upload pictures programatically into your image gallery, or documents to the document library you can use the following code snippets.


Images


Images can be uploaded to the document library as well as to the image gallery. Uploading to the Image Gallery is easy, just use this code snippet here:




IGImage addImage =
IGImageLocalServiceUtil.addImage(userId, folderId, imageName,
description, file, contentType, new ServiceContext());



You have to provide the userId of the User uploading. If you don´t know your userId, you can read here how to get it:



http://liferay-blogging.blogspot.com/2011/01/how-to-get-liferay-user-when-you-are.html

The ID of the Folder you want to add your image to is the next thing you have to know. You can take a look into the database if you want to hard-code it (DLFolder is the table you should take a look in) or you could make it configurable in your portlets preferences.

The Image Name and description is something that relies on your imagination ;) ... the File object is something you have to have. You could use the ICEFaces component ice:inputFile to get it. The contentType is something like "image/jpg" - you can get it based on the file ending.

The last thing is the ServiceContext - and to be honest, I always use a new instance. If you know what exactly this ServiceContext does, drop me a hint, ok ?

There are plenty more methods to add Images to the Image Gallery - just browse the API, which you can fine here:

http://content.liferay.com/4.2/api/portal-service/com/liferay/portlet/imagegallery/service/IGImageLocalServiceUtil.html


Documents


You can add documents to the document library in the same way as you can add images to the image gallery: By taking the correct XYZLocalServiceUtil.
In this case it´s the DLFileEntryLocalServiceUtil:


DLFileEntry fileENtry= DLFileEntryLocalServiceUtil
.addFileEntry(userId, folderId,fileName, fileTitle,
"description", "",inputStream, fileLength, new ServiceContext());




I won´t explain everything in detail here - it works pretty much like the IGImageLocalServiceImpl. The API is available for you at:


http://docs.liferay.com/portal/5.2/javadocs/portal-service/com/liferay/portlet/documentlibrary/service/DLFileEntryLocalServiceUtil.html

Related Posts:

No responses yet for "How to programatically add images and documents to Liferay"

Post a Comment