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 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:


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:



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:


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.