1
votes

Some pages in my CQ5.5 application are being made with no 'jcr:content' node. Since the node does not exist the properties of these particular pages do not exist and cannot be fixed by users. I want to add a button that will allow a user to add the node if it is missing. I have been able to add a button "Fix Properties" that has a custom function attached that retrieves the existing node and I am able to check for the existence of the jcr:content node. I cannot find documentation anywhere on how to make a node from scratch. I figured this would be a common thing but there is very little to find about it.

I found this documentation for Nodes in general and most of the functions work but so far no dice: http://dev.day.com/docs/en/cq/current/widgets-api/index.html?class=CQ.Ext.data.Node

and so far have this function. Each attempt was done separately:

CQ.wcm.SiteAdmin.createProperties = function() {
   var path = this.getCurrentPath();
   var tree = CQ.Ext.getCmp(this.id + "-tree");
   //var nodet = tree.getSelectionModel().getSelectedNode(); //gets the selected node
   //nodet.childNodes[i] skips over jcr:content nodes
   var nodej = CQ.Util.eval(path + '.2.json'); //am currently using this to get the node data instead
   if(typeof nodej['jcr:content'] == 'undefined')  //If the jcr:content node does not exist
   {
     //Attempt 1: 
       nodej['jcr:content'] = new CQ.wcm.Node({ 
           jcr:primaryType: "cq:PageContent",
           jcr:title: "title"});
     //Attempt 2: 
       nodej['jcr:content'] = new Node({  //Note no 'CQ.wcm'
           jcr:primaryType: "cq:PageContent",
           jcr:title: "title"});
     //Attempt 3:
     nodej.appendChild( new CQ.wcm.Node({ 
           jcr:primaryType: "cq:PageContent",
           jcr:title: "title"}));
     //Attempt 4:
     nodej.appendChild( new CQ.wcm.Node({ 
           Name : "jcr:content",              
           jcr:primaryType: "cq:PageContent",
           jcr:title: "title"}));
    }

};

None of the above attempts do anything but it is the closest thing I could think of.

I did find this question: How to add new property to JCR node through CQ.extjs? but it did nothing visible and it is also about adding to an existing node.

1

1 Answers

2
votes

All your attempts modify the Javascript object nodej, but there is no mechanism to reflect these changes in the repository automatically. That's why the changes were not saved. In order to create new node, you need to send a HTTP POST request:

CQ.HTTP.post(path + "/jcr:content", null, {
  "jcr:primaryType" : "cq:PageContent",
  "jcr:title" : "Title"
});

This request will by handled by the Sling framework (more precisely, by the SlingPostServlet), which will create a new resource.

Sidenote: please notice that you need to quote key name in the Javascript associative arrays if it contains a special character, like :. Therefore, following code will work:

console.log({
    "jcr:primaryType" : "cq:PageContent"
});

but this one will cause a syntax error:

console.log({
    jcr:primaryType : "cq:PageContent"
});