How to get extension properties of a user task using REST API

Hi,

I have added couple of properties to the Extensions tab of a user task and deployed the process. I am trying to get/access these extension properties using REST API, but I am unable to see these properties in the response JSON. Can someone tell me how to get these properties using REST API? Is it possible or not?

Thanks,
Ramu

You have to parse the BPMNs xml
There is no API to get the values. You have to get the xml using the API and then parse the xml.

1 Like

You have to parse the BPMNs xml
There is no API to get the values. You have to get the xml using the API and then parse the xml.

As Stephen said, that applies to a lot of values that should imo be retrievable using the API. Unfortunately that is not the case, but you can check the BpmnScanner inside the vPAV to see how you can easily retrieve values.

Especially this line should be interesting for you vPAV/src/main/java/de/viadee/bpm/vPAV/BpmnScanner.java at e028f85485b65ae50614907d2f5bafd883c1442f · viadee/vPAV · GitHub

Cheers,
Sascha

Thank you all for the reply. Will try parsing XML.

If you are using JavaScript with the API, take a look at the work done in Camunda to ASCIIDoctor documentation generator. They were parsing extension properties using the bpmn js libs

Thank you for the shout out.

I’m wrapping up the code with some instructions to take this from research project to a somewhat usable utility.


Virus-free. www.avast.com

I found a way to get extended data on the UserTask node。

private String getUserTaskExtensionByName(String taskId, String key) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(task.getProcessDefinitionId());
System.out.println(task.getProcessDefinitionId());
// 遍历该流程模型中的用户任务
Collection userTaskDefs = bpmnModelInstance.getModelElementsByType(UserTask.class);
for (UserTask userTaskDef : userTaskDefs) {
if (userTaskDef.getId().equals(task.getTaskDefinitionKey())) {
CamundaProperties extensionsProperty = userTaskDef.getExtensionElements()//
.getElementsQuery()//
.filterByType(CamundaProperties.class)//
.singleResult();
Collection camundaProperties = extensionsProperty.getCamundaProperties();
for (CamundaProperty property : camundaProperties) {
if (property.getCamundaName().equals(key)) {
return property.getCamundaValue();
}
}
}
}
return StringUtils.EMPTY;
}

  • Internally, this is also done by parsing the XML,
  • But internal cache processing has been added

public InstanceType findBpmnModelInstanceForDefinition(String definitionId) {
InstanceType bpmnModelInstance = instanceCache.get(definitionId);
if (bpmnModelInstance == null) {
DefinitionType definition = definitionCache.findDeployedDefinitionById(definitionId);
bpmnModelInstance = loadAndCacheBpmnModelInstance(definition);
}
return bpmnModelInstance;
}

In case someone finds this post via googling (as I did): our solution for this problem is to add a “start” execution listener to the start event, which sets the extension elements of the start event as process variables. In principle, you could extend the solution to set the extension elements of other tasks as process variables or add this listener to multiple tasks. We implemented the execution listener with Java, works on Camunda 7.13.

import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.BaseElement;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaProperties;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaProperty;
import java.util.ArrayList;
import java.util.Collection;

public class getExtensionPropsDelegate implements JavaDelegate {

    public void execute(DelegateExecution execution) {
        try{

			// Get base element
            BaseElement elem = execution
								.getProcessEngineServices()
									.getRepositoryService()
										.getBpmnModelInstance(execution.getProcessDefinitionId())
											.getModelElementById(execution.getCurrentActivityId());

            // Import CamundaProperties to query for them
            CamundaProperties extElem = elem.getExtensionElements().getElementsQuery().filterByType(CamundaProperties.class).singleResult();
            Collection<CamundaProperty> camPropCollection = extElem.getCamundaProperties();

            // Parse CamundaProperties Collection to ArrayList, otherwise Nashorn JavaScript engine can't handle collection type
            ArrayList<CamundaProperty> extElementsArray = new ArrayList<>(camPropCollection);

            // Loop through all extension elements
            for(int i=0 ; i < extElementsArray.size() ; i++){
                execution.setVariable(extElementsArray.get(i).getCamundaName(),extElementsArray.get(i).getCamundaValue());
            }
        } catch (Exception e){
            throw new BpmnError("GeneralProcessError","[getExtensionPropsDelegate.java]:There was an error parsing the extension properties.");
        }
    }
}
3 Likes

Thanks a lot for posting the solution - always really helpful :slight_smile: