Publishing documents using updater script

Hi Bloomreach Community,

We have a script that modifies some documents in the modified state and we want to run another script that can publish the modified state of these documents. How would we go about that? Is there a convenient function in the session workspace that we could use to publish documents? Or do we have to copy over the contents from the modified to the live node?

Thanks in advance for the help.

Hello, if you are using the EXIM plugin you can use the WorkflowDocumentManagerImpl for publishing. See the example here Hippo Content EXIM (EXport/IMport) Documentation – Tutorials - Importing Documents

1 Like

Or use something like below (gives you an example how to do things with workflow):

void updateParentDocuments(final Session session, String path) {
        try {
            final Node documentNode = session.getNode(path);
 
            HippoWorkspace workspace = session.getWorkspace() as HippoWorkspace
            WorkflowManager manager = workspace.getWorkflowManager()
            log.debug("Updating node: {}", path);
 
            if (!documentNode.isLocked()) {
                Node handle = documentNode.getParent()
                DocumentWorkflow documentWorkflow = manager.getWorkflow("editing", handle) as DocumentWorkflow;
                Document draftDocument = documentWorkflow.obtainEditableInstance()
                Node draftNode = draftDocument.getNode(session)
                changeDocument(draftNode)
                // save draft changes
 
 
                if (documentWorkflow.isModified()) {
                    session.save();
                    // commit draft changes
                    documentWorkflow.commitEditableInstance();
                }
 
                // publish draft changes:
                final Map<String, Serializable> hints = documentWorkflow.hints();
                // check if we can publish:
                final Boolean canPublish = hints.get("publish");
                if (canPublish) {
                    documentWorkflow.publish();
                } else {
                    log.info("Cannot publish document, no changes");
                }
            }
 
        } catch (RepositoryException e) {
            log.error("Error saving document", e);
            // loose changes
            session.refresh(false);
        }
        
    }
 
    private void changeDocument(Node draftNode) {
        String someString = "my draft changes" + Math.random()
        log.info("someString {}", someString);
        draftNode.setProperty("website:assets", someString)
    }
1 Like

Unfortunately we don’t have the EXIM plugin already installed, so we’ll try out the solution machak suggested, but thank you both regardless!