Expand my Community achievements bar.

[AEM6.5] ItemExistException while moving the existing node through workflow


Level 6

Hi All,
I have created a workflow to move/archive a node from one loaction to another location, but in same hirearchy, if hirearchy is not available it will create the same hirearchy.
example if we run workflow for /content/project/website/en-us/parent/child. it will create the same hirearchy under /content/archived and move the "child" node. /content/archived/website/en-us/parent/child


The problem I am facing here let say If I pass a payload /content/project/website/en-us/parent/child and run workflow it will successfully move node [child] from one loaction to another, and create the same hirearchy. 

But again if I need to move the parent it is showing me ItemExistException. because in above case it is alreadry created,

Could you please guide me the optimize way to resolve this, I have mentioned to TODO in code but not sure is it optimized way to achieve, so need guidance here.

I have used  : JcrUtils.getOrCreatedByPath(movedPath, "cq:page", session ) 

    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        String payload = workItem.getWorkflowData().getPayload().toString();
        ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);
        Page rootPage = Objects.requireNonNull(resourceResolver.getResource(payload)).adaptTo(Page.class);
        if (rootPage != null && payload.startsWith("/content/project")) {
            List<String> list = new ArrayList<>();
            Iterator<Page> childPages;
            childPages = rootPage.listChildren(new PublishedPageFilter(), true);
            if (childPages != null) {
                while (childPages.hasNext()) {
                    Page page = childPages.next();
            String[] paths = list.toArray(new String[0]);
            try {
                Node payloadNode;
                if (session != null) {
                    payloadNode = session.getNode(payload);
                    String payloadName;
                    if (payloadNode != null) {
                        payloadName = payloadNode.getName();
                        if (StringUtils.isNoneBlank(payload)) {
                            String archivePath = StringUtils.substringAfter(payload, "/content/project");
                            archivePath = StringUtils.substringBeforeLast(archivePath, "/");
                            if (StringUtils.isNoneBlank(archivePath)) {
                                archivePath = "/content/archived" + archivePath;
                                Node archived = JcrUtils.getOrCreateByPath(archivePath, "cq:Page", session);
                                //TODO check if payload node exist, if yes iterate all child node including jcr:content and move and remove from existing source.
                                session.move(payloadNode.getPath(), archived.getPath() + "/" + payloadName);

            } catch (RepositoryException e) {
                LOG.error("RepositoryException exception occurred at :: {}", Arrays.toString(paths));
            } finally {
                if (resourceResolver.isLive()) {
        } else {
            LOG.error("Workflow Terminated ");


1 Reply


Level 6

Thank you @lukasz-m  and @arunpatidar  for guiding me based on your previous response I got the way to do this :

String targetPath = archived.getPath() + "/" + payloadName;
if (session.nodeExists(targetPath)) {
    NodeIterator nodesIterator = payloadNode.getNodes();
    while (nodesIterator.hasNext()) {
        Node childNode = nodesIterator.nextNode();
        if (!targetPath.equals(childNode.getPath())) {
    if (!childNodes.isEmpty()) {
        for (Node childNode : childNodes) {
            session.move(childNode.getPath(), targetPath + DELIMITER_SLASH + childNode.getName());
} else {
    session.move(payloadNode.getPath(), archived.getPath() + DELIMITER_SLASH + payloadName);
page footer