Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

using the sling post servlet to create new nodes under a cq:Page

Avatar

Level 5

I have a cq:Page at /etc/news/users, this has a sling resourceTyle /news/components/admin/userlist which is a child type of wcm/foundation/component/page--body.html has a simple form (see below) in it right now. That page has a number of children representing virtual users or sling:resourceType:news/components/admin/user (essentially, these are legacy authors and contacts from an old news system that don't need accounts, but need some place to store their information like active authors).

I'm trying to create a form to create new users under this page. I thought I'd be able to get away with using the default sling post servlet to generate these nodes and not have to create a servlet myself, but I'm running into some issues. Do I have to create this servlet automatically? Am I missing some required fields?

Here's a slimmed down version of the form:

<form method="POST" action="/etc/news/users/"> <input id="name" type="text" name=":name" ng-value="displayName | spaceless | lowercase"/> <input id="displayName" type="text" name="displayName" ng-model="displayName"/> <input id="sling:resourceType" type="hidden" name="sling:resourceType" value="news/components/admin/user"/> <input type="submit" value="Create New User"> </form>

When I submit that form, I get a 500 error in the browser (when I enter "test" for the displayName --angular automatically places "test" in :name also):
 

Error while processing /etc/news/users/test Status 500 Message org.apache.sling.api.SlingException: Exception during response processing. Location /etc/news/users/test Parent Location /etc/news/users Path /etc/news/users/test Referer http://localhost:4502/etc/news/users.html

and a javax.jcr.nodetype.ConstraintViolationException in the log:

27.06.2016 11:13:17.733 *ERROR* [0:0:0:0:0:0:0:1 [1467040397731] POST /etc/news/users/ HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing. javax.jcr.nodetype.ConstraintViolationException: No matching property definition: displayName = test at org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.setProperty(NodeDelegate.java:522) at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1375) at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1363) at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:208) at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112) at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1363) ....
1 Accepted Solution

Avatar

Correct answer by
Level 10

I have found default sling servlet not to be that reliable for creating resources - I have much preferred using Strongly -typed API such as Sling API, JCR API, etc.  (this subject may make a great Ask the AEM COmmuntiy webinar)

View solution in original post

10 Replies

Avatar

Level 5

Also, when I remove the displayName property above, I get:

27.06.2016 11:33:20.518 *ERROR* [0:0:0:0:0:0:0:1 [1467041600516] POST /etc/news/users/ HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing. javax.jcr.nodetype.ConstraintViolationException: No matching property definition: sling:resourceType = news/components/admin/user

So I don't think it's an issue with the displayName field itself.

Avatar

Level 10

I'm trying to create a form to create new users under this page. I thought I'd be able to get away with using the default sling post servle

I would personally stay away from the default sling post servlet and create a Sling Servlet that uses the Jackrabbit Strongly Typed User Manager API to create users. 

Using Jackrabbit UserManager APIs to create AEM Users and Groups

Avatar

Level 5

Thought the user manager might be the best option for my particular use case with users--removing it from that context, I'm still curious if anyone could help me understand why my solution isn't working.

Avatar

Level 10

Why would strongly typed API not be best way to proceed in this requirement? 

Avatar

Level 5

I'm trying to understand this outside of the requirement to create users--ie: what if I just wanted to create a sub resource below my path? That doesn't' seem to be working, and I'm not sure why.

Avatar

Correct answer by
Level 10

I have found default sling servlet not to be that reliable for creating resources - I have much preferred using Strongly -typed API such as Sling API, JCR API, etc.  (this subject may make a great Ask the AEM COmmuntiy webinar)

Avatar

Level 5

Yeah, my exploration suggests that there are some implicit requirements that are hard to determine -- I was able to create another page at the /etc/news level -- that parent is a folder, but I still haven't figured out how to create a node under  /etc/news/users, so I think cq:Page has some restraints or requirements on what can be placed underneath it.

The APIs are an option, I just wanted to try to do this all in front-end sightly.

Avatar

Level 2

Hi B Stockwell,,

The issue is most probably due to request trying to  create nodes directly under cq:Page node type.

Possible solutions : 

Solution#1 :

Submit form to :/etc/news/users/jcr:content/ instead of /etc/news/users/

Solution#2 :

Create page for each new user under /etc/news/users/

with following request:

Ref. : http://www.wemblog.com/2011/09/how-to-create-pages-using-curl-command.html

Hope it helps. Take care.

Avatar

Level 10

For the default API to work, I believe the node should be available to create the properties. 

Instead as @scott mentioned, I would go with Typed APIs to handle this which gives much more control.

Avatar

Level 10

Yes, I too agree with Scott's statement, Go with API's such as JCR API's Sling API's  to create the node/resource than default Sling servlets!

Thanks,
Ratna Kumar.