Magnolia relies on the JCR API to manage its data, with security handled by JCR.

Important Util classes

For your convenience, four Util classes have been implemented:

These are Util classes that mainly catch exceptions or wrap some JCR API calls.

Accessing a workspace

  • Access a workspace with javax.jcr.Session:

    Session session = MgnlContext.getJCRSession("workspaceName");
    Session session = someNode.getSession();

Getting javax.jcr.Node or javax.jcr.Property

  • Access a specific node or property with the JCR session:

    Node foundNode = session.getNodeByIdentifier("identifier");
    Node foundNode = session.getNode("absolutePath");
    Property foundProperty = session.getProperty("absolutePath");
    Node foundNode = NodeUtil.getNodeByIdentifier("workspaceName", "identifier");

Operations on javax.jcr.Node

  • Get a node’s name:

    String name = someNode.getName();
    String name = NodeUtil.getName(someNode);
  • Get all child nodes together:

    NodeIterator childrenIterator = someNode.getNodes();
    Iterable<Node> childrenIterable = NodeUtil.asIterable(childrenIterator);
    List<Node> childrenList = NodeUtil.asList(childrenIterable);
    Iterable<Node> childrenIterable = NodeUtil.collectAllChildren(someNode);
    List<Node> childrenList = NodeUtil.asList(childrenIterable);
  • Get a specific child node by name:

    Node childNode = someNode.getNode("relativePath");
  • Get a node property by name:

    Property nodeProperty = someNode.getProperty("relativePath");
    Property foundProperty = PropertyUtil.getProperty(someNode, "relativePath");
  • Get a node’s path:

    String pathToNode = someNode.getPath();
    String pathToNode = NodeUtil.getNodePathIfPossible(someNode);
  • Create a property and set its value for a specific node:

    someNode.setProperty("propertyName", "propertyValue");
    PropertyUtil.setProperty(someNode, "propertyName", "propertyValue");
  • Create a subnode:

    someNode.addNode("relativePath", primaryNodeTypeName);
    NodeUtil.createPath(parent, "relativePath", primaryNodeTypeName)
  • Get a session from a node and save changes:

    //Get a session from a node and save changes
    //If you already have the session

Content node metadata

  • Examples of content node metadata with NodeTypes:

    NodeTypes.Activatable.update(node, "username", isActivated);
    NodeTypes.Renderable.set(node, "template id");
    NodeTypes.LastModified.update(node, "username", date);

Operations on javax.jcr.Property

  • Get a property’s name:

    String name = nodeProperty.getName();
  • Get a property’s path:

    String pathToProperty = nodeProperty.getPath();
  • Get a property’s parent node:

    Node parentNode = nodeProperty.getParent();
  • Get a value from a property:

    String stringValue = nodeProperty.getString()
    Calendar dateValue = nodeProperty.getDate();
    Double doubleValue = nodeProperty.getDouble();
    Long longValue = nodeProperty.getLong();
    Binary forBinaryNodeData = nodeProperty.getBinary();
    PropertyUtil.getString(someNode, propertyName);
    PropertyUtil.getDate(someNode, propertyName);
    PropertyUtil.getBoolean(someNode, propertyName, defaultValue)
  • Set the value of a property:


Exception-free JCR operations

  • Use JcrFunctions to work with JCR items without catching repository exceptions:

    Set<Node> parents = getValueContext().get()

Providing a node as ContentMap to scripts

It is not recommended that you operate in template scripts directly on a JCR Node. cmsfn provides all scripts with any node transformed into ContentMap. That way, it is much easier to access a node’s data. ContentMap provides the following special attributes that are not properties:

  • @name

  • @path

  • @id

  • @depth

  • @nodeType

    In Java:

    ContentMap nodeAsContentMap = new ContentMap(someNode);
    Node backToNode = nodeAsContentMap.getJCRNode();

    In Freemarker:


    See cmsfn for more templating functions operating on the JCR API.

Related topics