{"id":12509,"date":"2014-04-07T12:36:36","date_gmt":"2014-04-07T07:06:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12509"},"modified":"2017-02-06T10:37:48","modified_gmt":"2017-02-06T05:07:48","slug":"custom-node-type-in-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-node-type-in-aem\/","title":{"rendered":"Creating Custom Node Type in JCR"},"content":{"rendered":"<p>In one of our project, we had to create <strong>custom node types<\/strong>. In this blogpost, we&#8217;ll talk about the various ways of creating the Custom Node Type and deploying it across multiple instances. We&#8217;ll be using AEM 5.6.1 as our CQ server.<\/p>\n<p><strong>A. Creating and Registering the Custom Nodetype\u00a0<\/strong><strong><br \/>\n<\/strong><\/p>\n<p>There are broadly following three ways of creating custom node types.<\/p>\n<ol>\n<li>Using Node Type Administration console.<\/li>\n<li>Programmatically<\/li>\n<li>Using Package Manager<\/li>\n<\/ol>\n<p>We&#8217;ll discuss them one by one :<\/p>\n<ol>\n<li><strong>Using\u00a0Node Type Administration Console\u00a0<\/strong>\n<ol>\n<li><strong>Using CND files.<\/strong><\/li>\n<\/ol>\n<p style=\"font-weight: inherit;\">The Compact Namespace and Node Type Definition (CND) notation provides 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.\u00a0<a href=\"https:\/\/jackrabbit.apache.org\/node-type-notation.html\" target=\"_blank\">Existing documentation<\/a>\u00a0can be followed for creating the CND file.<\/p>\n<p>Go to\u00a0Node Type Administration\u00a0console, click on\u00a0<strong>Import \u00a0Node Type,<\/strong>\u00a0copy\/paste the CND file in the textarea, keep &#8220;<strong>Automatically register nodetype<\/strong>&#8221; checkbox and\u00a0<strong>&#8220;Automatically register defined namespaces&#8221;<\/strong>\u00a0checked. Click on submit and your custom node type will be\u00a0registered.<\/p>\n<ol start=\"2\">\n<li><strong>Without using CND files<\/strong><\/li>\n<\/ol>\n<p style=\"font-weight: inherit;\">Go to\u00a0Node Type Administration\u00a0console,click on\u00a0<strong>Create Node Type<\/strong>\u00a0and enter the details about Node Name , child Node defintions , property definitions , supertypes etc. Click on the\u00a0<em>[Register Node Type]<\/em>\u00a0link at the bottom of the page to register this newly created Node type. Check the nodetype in Node type Administration console. Below is the screenshot for more details :<\/p>\n<\/li>\n<li><strong>Programmatically<\/strong><br \/>\nWe can register the nodetype programmatically as well.<\/p>\n<ul style=\"font-weight: inherit;\">\n<ul style=\"font-weight: inherit;\">\n<li><strong>Using CND file.<\/strong><\/li>\n<\/ul>\n<\/ul>\n<p>We can use\u00a0JCR Commons CndImporter\u00a0to register it. Following is the code snippet to regsiter it. Create a CND file say <code>nodetypes.cnd<\/code> having the definition of the new node type. Make this file as a part of the bundle.<\/p>\n<p>[java]public class CustomNodeTypeExample {<br \/>\n\/\/ some code<br \/>\nsession = slingRepository.loginAdministrative(null);<br \/>\nInputStream fis = CustomNodeTypeExample.class.getResourceAsStream(&amp;amp;quot;\/nodetypes.cnd&amp;amp;quot;);<br \/>\n NodeType[] nodeTypes = CndImporter.registerNodeTypes(new InputStreamReader(fis), session);<br \/>\n for (NodeType nt : nodeTypes) {<br \/>\n LOGGER.info(&amp;amp;quot;Registered: &amp;amp;quot; + nt.getName());<br \/>\n }<br \/>\n session.save();<br \/>\n\/\/ some code<br \/>\n}<br \/>\n[\/java]<\/p>\n<ul style=\"font-weight: inherit;\">\n<ul style=\"font-weight: inherit;\">\n<li><strong>Without using CND file.<\/strong><\/li>\n<\/ul>\n<\/ul>\n<p>We can use\u00a0JCR API to create a new node type and\u00a0register it. Following is the code snippet to register it.<\/p>\n<p>[java]session = slingRepository.loginAdministrative(null);<br \/>\n NodeTypeManager manager = (NodeTypeManager)session.getWorkspace().getNodeTypeManager();<br \/>\nNamespaceRegistry ns=session.getWorkspace().getNamespaceRegistry();<br \/>\nns.registerNamespace(&amp;amp;quot;ig&amp;amp;quot;,&amp;amp;quot;http:\/\/www.intelligrape.com\/CustomNode&amp;amp;quot;);<br \/>\n\/\/ Create node type<br \/>\n NodeTypeTemplate nodeTypeTemplate = manager.createNodeTypeTemplate();<br \/>\n nodeTypeTemplate.setName(&amp;amp;quot;ig:testNodeType&amp;amp;quot;);<br \/>\n\/\/ Create a new property<br \/>\n PropertyDefinitionTemplate customProperty1 = manager.createPropertyDefinitionTemplate();<br \/>\ncustomProperty1.setName(&amp;amp;quot;ig:Name&amp;amp;quot;);<br \/>\ncustomProperty1.setRequiredType(PropertyType.STRING);<br \/>\nPropertyDefinitionTemplate customProperty2 = manager.createPropertyDefinitionTemplate();<br \/>\ncustomProperty2.setName(&amp;amp;quot;ig:City&amp;amp;quot;);<br \/>\ncustomProperty2.setRequiredType(PropertyType.STRING);<br \/>\n \/\/ Add property to node type<br \/>\nnodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty1);<br \/>\nnodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty2);<br \/>\n\/* Register node type *\/<br \/>\n manager.registerNodeType(nodeTypeTemplate, true);<br \/>\n session.save(); [\/java]<\/li>\n<li><strong>Using\u00a0Package Manager<\/strong><\/li>\n<\/ol>\n<p>We can register node type via package manager as well . In Package Manager, upload a CQ package containing custom <code>nodetypes.cnd<\/code>\u00a0 and install it. Check that the custom nodetypes are registered in <strong>Node Type Administration<\/strong> console.<\/p>\n<p><strong>Troubleshoot :\u00a0<\/strong><\/p>\n<ul>\n<ul>\n<li>After registering the nodetype, make sure it is visible in\u00a0<strong>Node Type Administration<\/strong>\u00a0console. If not registered, check the <code>error.log<\/code> for more insight.<\/li>\n<li>CND file should be in proper format to avoid unwanted errors.<\/li>\n<li>Java 7 introduced a stricter verification and changed the class format a bit &#8212; to contain a stack map, used to verify that code is correct.\u00a0If you are using java 7, pass these parameter\u00a0<code>-XX:MaxPermSize=512m -Xmx1520m -XX:-UseSplitVerifier<\/code>\u00a0while starting the instance from command line. Refer this <a href=\"http:\/\/help-forums.adobe.com\/content\/adobeforums\/en\/experience-manager-forum\/adobe-experience-manager.topic.html\/forum__o0nw-i_created_anosgibu.html\" target=\"_blank\">link<\/a>\u00a0for more details.<\/li>\n<\/ul>\n<\/ul>\n<p><strong>B. Deploying the Custom Nodetype across multiple instances<\/strong><\/p>\n<p>If we have enabled clustering, then our multiple author 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 \u00a0done via two ways :<\/p>\n<ol>\n<li>If we are programmatically registering the new node type , then deploying the bundle will simply make it visible across all the instances.<\/li>\n<li>Whenever a new node type gets registered in repository, three files gets updated . <code>custom_nodetypes.xml<\/code> at <code>&lt;CQ author instance directory&gt;\/crx-quickstart\/repository\/repository\/nodetypes<\/code>\u00a0 will contain the definition of new node type. <code>ns_idx.properties<\/code> and <code>ns_reg.properties<\/code> at\u00a0<code>&lt;CQ author instance directory&gt;\/crx-quickstart\/repository\/repository\/namespaces<\/code> 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.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In one of our project, we had to create custom node types. In this blogpost, we&#8217;ll talk about the various ways of creating the Custom Node Type and deploying it across multiple instances. We&#8217;ll be using AEM 5.6.1 as our CQ server. A. Creating and Registering the Custom Nodetype\u00a0 There are broadly following three ways [&hellip;]<\/p>\n","protected":false},"author":110,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":70},"categories":[1],"tags":[4847,1207,1360],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12509"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12509"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12509\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}