Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Constraint Violation Exception when trying to add node property

Avatar

Level 3

Hi, I have created a WCMUsePojo to handle form submissions that serve to update and/or create user profile properties found under

/home/users/../userID/profile

But am getting a Constraint Violation exception

javax.jcr.nodetype.ConstraintViolationException: No matching property definition: nodePropertyValue = thisissomestring

Even though this node is of type unstructured. I have posted my code below, any insight would be appreciated!

Thank you!

Session session = getRequest().getResourceResolver().adaptTo(Session.class);
UserManager userManager = getRequest().getResourceResolver().adaptTo(UserManager.class);
String sessionUserId = session.getUserID();
authorizablePath = userManager.getAuthorizable(sessionUserId).getPath();

String nodePropertyValue= getRequest().getParameter("nodePropertyValue");

Resource resource = getRequest().getResourceResolver().getResource(authorizablePath);
Node node = resource.adaptTo(Node.class);
Node profile = node.getNode("profile");

profile.setProperty("nodePropertyName", nodePropertyValue);
session.save();

The form is structured as such

<form method="POST" action="FormSubmission.java">

  <input type="text" name="nodePropertyValue" id="nodePropertyValue" />

  <button type="submit">Submit</button>

</form>

1 Accepted Solution

Avatar

Correct answer by
Level 10

"No matching property definition: nodePropertyValue = thisissomestring"

means that your code is trying to add "nodePropertyValue" to a node <authorizablePath> where it's not allowed.

Not sure why it says the property name is not "nodePropertyName" but "nodePropertyValue = thisissomestring". You should run the server in debug mode or dump logs to find more clues.

you're correct that "nt:unstructured" should've allowed it but I'm not sure if you as a logged-in user can modify your own (session-user's profile) profile at runtime? Did you try with "admin" user? Could you check if this user has "write" permissions (rep:write + jcr:write) to create the property on own profile (although that would've thrown a different exception in logs)

You may try with a service user and ensure that you have appropriate permissions on the /home/users... path

P.S. As Scott mentioned, you should use better options for this use case in production-ready code

View solution in original post

6 Replies

Avatar

Level 10

Using a WCMUsePojo not best choice to handle a form submission. Where did you get that idea?

Avatar

Level 10

Or by form submission, do you mean component dialog?

Avatar

Correct answer by
Level 10

"No matching property definition: nodePropertyValue = thisissomestring"

means that your code is trying to add "nodePropertyValue" to a node <authorizablePath> where it's not allowed.

Not sure why it says the property name is not "nodePropertyName" but "nodePropertyValue = thisissomestring". You should run the server in debug mode or dump logs to find more clues.

you're correct that "nt:unstructured" should've allowed it but I'm not sure if you as a logged-in user can modify your own (session-user's profile) profile at runtime? Did you try with "admin" user? Could you check if this user has "write" permissions (rep:write + jcr:write) to create the property on own profile (although that would've thrown a different exception in logs)

You may try with a service user and ensure that you have appropriate permissions on the /home/users... path

P.S. As Scott mentioned, you should use better options for this use case in production-ready code

Avatar

Community Advisor

agree with above comments: please use service user, as you are trying to do some changes with node at runtime. ResourceResolverFactory rrFactory; Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver"); ResourceResolver resourceResolver = rrFactory.getServiceResourceResolver(param); also if you are working on normal page form then you can sling models. search for helpx articles.

Avatar

Level 3

Thanks Gaurav, I was not planning on using the WCMUsePojo in production ready code but simply experimenting in a quick fashion to understand what logic should be used to process form entries into node property updates. Also, "nodePropertyName" and "Value" were my fillers to help explain what was previously there "email" and "example@gmail.com." I have moved my code into an AEM core bundle and will utilize JSP to return my param values.

Thanks!