How to save documents to document-type restricted folder programmatically

thumbnail
Jamie Sammons, modified 2 Years ago. New Member Posts: 18 Join Date: 8/3/22 Recent Posts

Good morning People,

 

I am trying to create a contract-manager. I want to upload files, add some additional information like "category", "subcategory" and "expireDate".

To store the files, I created a folder in "Documents and Media", created a "Metadata Set" and a "Document Type" and applied it to the Folder. 

The values for "subcategory" are dependent on the value of "category" and should be dynamically loaded into the subcategory-dropdown. I thought I need to create custom inputs/form in a custom portlet. Now I want to upload the Document along with the additional information, but I cant figure out how to include the custom fields.

I tried adding them to the serviceContext but actually this is just copied code and I am not sure if any of this makes sence. The Fieldname is "SelectFromList09610918" and the error message claims, its empty so my guess is, something with adding it to service context does not work.

			// Define fields map
			Fields fields = new Fields();

			// Put your values (I don't remember why I had to put these brackets and quotes, it just worked this way :) )
			fields.put(new Field(
			  "SelectFromList09610918",
			  StringPool.OPEN_BRACKET + StringPool.QUOTE +  "category" + StringPool.QUOTE + StringPool.CLOSE_BRACKET
			  ));

			// Obtain your DDLFileEntryType and its structure (in my case it was first element)
			DLFileEntryType fileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(groupId, 
									"45963");

			DDMStructure ddmStructure = fileEntryType.getDDMStructures().get(0);
			
	    	ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest);
	    	
	    	// Set document's type
			serviceContext.setAttribute("fileEntryTypeId", fileEntryType.getPrimaryKey());
	    	
			// Put fields to service context
			serviceContext.setAttribute(Fields.class.getName() + ddmStructure.getStructureId(), fields);
	    	
	    	//save file to Documents&Files app
	    	FileEntry fileEntry = DLAppServiceUtil.addFileEntry( sourceFileName, groupId, folder.getFolderId(), sourceFileName, mimeType, filenameField, URLEncoder.encode(filenameField, StandardCharsets.UTF_8.toString()), description, changeLog, file, null, null, serviceContext);

How can I add these fields now? Is there a better approach?
I appreciate every idea and hint, also if you think I had a wrong decision let me know :)

Greetings