How to put your document library folder under workflow control programmatically

If you want to create document library folders (dlfolder) programmatically, you can do this very easy by calling
DLFolderLocalServiceUtil.addFolder(userId, groupId, groupId, false, 0, "Folder title", "Folder description",false, new ServiceContext());
That´s just one line of code and you´re done. But what if you want to put your folder under workflow control? Liferay offers the possibility to connect assets in your folders to specific workflows. So as soon as you upload a basic document for example, the workflow will be started.  You can do that manually when you select your folder and click on "Edit":

Wouldn´t it be nice it that could be done programmatically ? Liferay source code comes to the rescue ! This is how they do it. First, set the file entry types override:
dlFolder = DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
Then you need to query the DLFileEntryType table for the FileEntryType you want to set up a workflow for. Next, you need to set the DlFileEntries for the folder:
long[] fileEntryTypeIds = new long[] { basicDocumentType.getFileEntryTypeId() };
DLFileEntryTypeLocalServiceUtil.setDLFolderDLFileEntryTypes(dlFolder.getFolderId(), fileEntryTypeIds);
Next is a query to get the process definitions you want your folder and the fileEntryType to connect with:
List<WorkflowDefinition> activeWorkflowDefinitions = WorkflowDefinitionManagerUtil.getActiveWorkflowDefinitions(companyId, "Your Process Name", 0, 100, null);
And last but not least you need to add a workflow definition link:
WorkflowDefinitionLinkLocalServiceUtil.addWorkflowDefinitionLink(userId, companyId, groupId, 
DLFolderConstants.getClassName(), dlFolder.getFolderId(), basicDocumentType.getFileEntryTypeId(),  name, version);

If you like 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.