Thursday, 23 February 2017

Adding a list of SDL Web (Tridion) components to Workflow using the SDL Web Core Service in Java

Here is a code snippet demonstrating how a list of updated components can be added to a new Workflow instance in SDL Web using the Core Service API.

protected void doWorkflow(List<IdentifiableObjectData> updatedComponents) throws Exception {
if (updatedComponents.isEmpty())
return;
try {
// Build the array of components
ArrayOfstring arrayOfComponents = new ArrayOfstring();
for (final IdentifiableObjectData component : updatedComponents) {
arrayOfComponents.getString().add(component.getId().getValue());
}
// Start the bundle workflow activity
StartWorkflowInstructionData startWorkflowInstructionData = new StartWorkflowInstructionData();
// Provide a process title
ObjectFactory factory = new ObjectFactory();
startWorkflowInstructionData.setProcessInstanceTitle(
factory.createStartWorkflowInstructionDataProcessInstanceTitle("Workflow instance created by the Updater Script"));
ProcessInstanceData process = Tridion.INSTANCE.getService()
.startWorkflow(publication.getId(), startWorkflowInstructionData, readBackOptions);
// Add components to workflow
Tridion.INSTANCE.getService().addToWorkflow(arrayOfComponents,
process.getActivities().getValue().getActivityData().get(0).getId().getValue(), readBackOptions);
log.info("Components added to workflow successfully!");
}
catch(Exception e) {
log.error("Exception adding components to Workflow", e);
}
}

First step to building a Java SDL Web (Tridion) Core Service Client

I've recently written a SDL Web (Tridion) Core Service client in Java. I was able to get started quite quickly by using the jaxws-maven-plugin as shown below.

<dependencies>
<!-- This is for the JAXWS WsImport -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlUrls>
<wsdlUrl>http://domain/webservices/CoreService2013.svc?wsdl</wsdlUrl>
</wsdlUrls>
</configuration>
<!-- if you want to use a specific version of JAX-WS, you can do so like this -->
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.2.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${project.build.directory}/endorsed</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/endorsed</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.7</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.9</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The classes will be generated inside the /target/generated-sources/wsimport/ folder in your project. You only need to assign this folder as a source folder in your IDE and start coding!