April 10, 2020
Estimated Post Reading Time ~

Creating Custom Node Type in JCR

In one of our projects, we had to create custom node types. In this blog post, we’ll talk about the various ways of creating the Custom Node Type and deploying it across multiple instances. We’ll be using AEM 5.6.1 as our CQ server.  

A. Creating and Registering the Custom Nodetype
There are broadly following three ways of creating custom node types.

  1. Using the Node Type Administration console.
  2. Programmatically
  3. Using Package Manager
We’ll discuss them one by one :
  1. Using the Node Type Administration Console 
    1. Using CND files.
    The Compact Namespace and Node Type Definition (CND) notation provide a compact standardized syntax for defining node types and making namespace declarations. The notation is intended both for documentation and for programmatically registering node types. Existing documentation can be followed for creating the CND file.
    Go to Node Type Administration console, click on Import  Node Type, copy/paste the CND file in the textarea, keep “Automatically register nodetype” checkbox and “Automatically register defined namespaces” checked. Click on submit and your custom node type will be registered.
    1. Without using CND files
    Go to Node Type Administration console, click on Create Node Type and enter the details about Node Name, child Node definitions, property definitions, supertypes, etc. Click on the [Register Node Type] link at the bottom of the page to register this newly created Node type. Check the nodetype in the Node type Administration console. Below is the screenshot for more details :
  2. Programmatically
    We can register the nodetype programmatically as well.
    • Using CND file.
    We can use JCR Commons CndImporter to register it. Following is the code snippet to register it. Create a CND file say nodetypes.cnd having the definition of the new node type. Make this file as a part of the bundle.

    public class CustomNodeTypeExample {
    // some code
    session = slingRepository.loginAdministrative(null);
    InputStream fis = CustomNodeTypeExample.class.getResourceAsStream("/nodetypes.cnd");
    NodeType[] nodeTypes = CndImporter.registerNodeTypes(new InputStreamReader(fis), session);
    for (NodeType nt : nodeTypes) {
    LOGGER.info("Registered: " + nt.getName());
    }
    session.save();
    // some code
     
    Without using the CND file.
    We can use the JCR API to create a new node type and register it. Following is the code snippet to register it.

    session = slingRepository.loginAdministrative(null);
    NodeTypeManager manager = (NodeTypeManager)session.getWorkspace().getNodeTypeManager();
    NamespaceRegistry ns=session.getWorkspace().getNamespaceRegistry();
    ns.registerNamespace("ig","http://www.intelligrape.com/CustomNode");
    // Create node type
    NodeTypeTemplate nodeTypeTemplate = manager.createNodeTypeTemplate();
    nodeTypeTemplate.setName("ig:testNodeType");
    // Create a new property
    PropertyDefinitionTemplate customProperty1 = manager.createPropertyDefinitionTemplate();
    customProperty1.setName("ig:Name");
    customProperty1.setRequiredType(PropertyType.STRING);
    PropertyDefinitionTemplate customProperty2 = manager.createPropertyDefinitionTemplate();
    customProperty2.setName("ig:City");
    customProperty2.setRequiredType(PropertyType.STRING);
    // Add property to node type
    nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty1);
    nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty2);
    /* Register node type */
    manager.registerNodeType(nodeTypeTemplate, true);
    session.save();

Using Package Manager
We can register node type via package manager as well. In Package Manager, upload a CQ package containing custom nodetypes.cnd  and install it. Check that the custom nodetypes are registered in the Node Type Administration console.
Troubleshoot : 
    • After registering the nodetype, make sure it is visible in the Node Type Administration console. If not registered, check the error.log for more insight.
    • CND files should be in the proper format to avoid unwanted errors.
    • Java 7 introduced a stricter verification and changed the class format a bit — to contain a stack map, used to verify that code is correct. If you are using java 7, pass these parameter -XX:MaxPermSize=512m -Xmx1520m -XX:-UseSplitVerifier while starting the instance from the command line. Refer to this link for more details.
B. Deploying the Custom Nodetype across multiple instances
If we have enabled clustering, then our multiple authors and publish instances will be running on separate machines. We would want the new node type to be visible in all the instances. It can be  done in two ways
  1. If we are programmatically registering the new node type, then deploying the bundle will simply make it visible across all the instances.
  2. Whenever a new node type gets registered in the repository, three files get updated. custom_nodetypes.xml at <CQ author instance directory>/crx-quickstart/repository/repository/nodetypes  will contain the definition of a new node type. ns_idx.properties and ns_reg.properties at <CQ author instance directory>/crx-quickstart/repository/repository/namespaces will have the details of the new namespaces added. Copy/Pasting these files to all the instances at the specified location will make it visible. Note that this will require an instance restart.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.