Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Setting value is multi values property through java code

Avatar

Level 2

Hi,

I am trying to set values to one string[] multi valued property in the CQ page.  i am doing it through remote java application not in CQ environment.

Here is the code that i am using

Value[] values = new Value[10];

values[0] = session.getValueFactory().createValue("NewValue-one");

  values[1] = session.getValueFactory().createValue("NewValue-two");

while(nodeIter.hasNext())

{

Node myNode= nodeIter.nextNode();

myNode.getName();

myNode.setProperty("cq:tags", values);

}

I am not getting any exceptions while setting the property but after setting the values to the property, i looked into the properties tab in the CRXDE and i am seeing the values are not set and it's empty.

Here is the screenshot of properties tab in the CRXDE

1362801_pastedImage_1.png

Can someone help me on this? Let me know if you need anymore info.

1 Accepted Solution

Avatar

Correct answer by
Level 5

In  myNode.setProperty("cq:tags", values); values has to be String[] and save session.

myNode.getSession().save();

1362833_pastedImage_0.png

Ref: https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Node.html#setProperty(java.lan..., java.lang.String[])

View solution in original post

10 Replies

Avatar

Correct answer by
Level 5

In  myNode.setProperty("cq:tags", values); values has to be String[] and save session.

myNode.getSession().save();

1362833_pastedImage_0.png

Ref: https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Node.html#setProperty(java.lan..., java.lang.String[])

Avatar

Level 10

That is correct - you need to use an array.

Avatar

Level 2

I agree with you. But In my case i am setting value to the "cq:tags" property.  whenever i am setting the values using the above code, the value becomes empty in the cq:tags property.

Do we need to do in a different way to set multivalue to the property cq:tags?

Avatar

Community Advisor

Edit :- Correcting my answer

Arun,

     Could you please check if the tags you are trying to set is already present under /etc/tags ? If not your values will never set to cq:tags property

Thanks

Veena

Avatar

Community Advisor

Hi Scott.

     We cannot set tags as normal properties. Those needs to be handled using TagManager API

Avatar

Level 5

Can you please confirm that your tags are already created before saving in cq:tags property. Properties will not save if tags are not present.

or you can also use tagManager api to set tags

void setTags(Resource resource, Tag[] tags)

void setTags(Resource resource, Tag[] tags,boolean autoSave)

Avatar

Level 2

I will check that . Before doing that i am trying to access tag manager api but can't get access to getTagManager method.

I can't able to call get TagManager method using the JCRTAG manager resolver factory. It's throwing me error. Here is my code

JcrTagManagerFactory.getTagManager(session); //throwing compile time error

@mjb54261515

Avatar

Community Advisor

Hi Arun

     For tag related operations, you should use tag manager API TagManager ("The Adobe AEM Quickstart and Web Application.") 

To get a TagManager object , you can do the following

Please refer sample codes to work with TagManagers here Java Code Example com.day.cq.tagging.TagManager

Java Code Example com.day.cq.tagging.TagManager

Thanks

Veena

Avatar

Level 2

Hey Guys,

Thanks for your valuable information. It was very helpful. At-last i found solution to my problem. I am connecting CQ remotely through my java code and i am not using Sling or OSGI . Because my application is not running in any of the CQ servers and it's running remotely in apache tomcat server. So i am using only JCR api to connect to CQ server and modifying the contents. Here is the code which used for tagging.

Node jcrNode=session.getNode("/etc/tags/default");

Node my_casecustom=jcrNode.addNode("my_casecustom","cq:Tag");

jcrNode.addMixin("mix:referenceable");

session.refresh(true);

    session.save();

    my_casecustom.setProperty("jcr:title", "custom,my case");

    my_casecustom.setProperty("jcr:title.en_us", "custom,my case");

    my_casecustom.setProperty("sling:resourceType", "cq/tagging/components/tag");

session.refresh(true);

    session.save();

After i added CQ:TAGS property into my pagecontent node and it's settings the values to corresponding node.