How to programmatically create a form

thumbnail
Anupam Shrivastava, modified 6 Years ago. New Member Posts: 3 Join Date: 7/24/15 Recent Posts
It is possible to programmatically create a form based on a specific element set.
This is done through the "DDMFormInstanceLocalServiceUtil.addFormInstance" method.
// Here goes your existing element set Id
long ddmStructureId = 33869;

// Initialize form instance
long ddmInstanceId = CounterLocalServiceUtil.increment(DDMFormInstance.class.getName());
DDMFormInstance ddmFormInstance = DDMFormInstanceLocalServiceUtil.createDDMFormInstance(ddmInstanceId);
ddmFormInstance.setStructureId(ddmStructureId);

// Retrieve the element set
DDMStructure ddmStructure = null;
try {
	ddmStructure = DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
} catch (PortalException e) {
	e.printStackTrace();
}

// Convert it into formValues needed for addFormInstance
DDMForm ddmForm = ddmStructure.getDDMForm();
DDMFormValues ddmFormValues = _ddmFormValuesFactory.create(actionRequest, ddmForm);

// Set name and description for the new form
Map<locale, string> name = new HashMap<locale, string>();
name.put(LocaleUtil.US, "New Form");
Map<locale, string> description = new HashMap<locale, string>();
description.put(LocaleUtil.US, "New Form Description");

// Create the form
try {
	DDMFormInstanceLocalServiceUtil.addFormInstance(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), ddmStructureId, name, description, ddmFormValues, serviceContext);
} catch (PortalException e) {
	e.printStackTrace();
}</locale,></locale,></locale,></locale,>

See attached sample code for more details.
Disclaimer: Please note that this is a sample of how to extend your portlet and should only be used as a reference.