Setting value is multi values property through java code | Community
Skip to main content
Arun_Chola
Level 2
December 1, 2017
Solved

Setting value is multi values property through java code

  • December 1, 2017
  • 10 replies
  • 11178 views

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

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by manoj_devapath

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

myNode.getSession().save();

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

10 replies

manoj_devapath
manoj_devapathAccepted solution
Level 5
December 1, 2017

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

myNode.getSession().save();

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

smacdonald2008
Level 10
December 1, 2017

That is correct - you need to use an array.

Arun_Chola
Level 2
December 4, 2017

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?

VeenaVikraman
Community Advisor
Community Advisor
December 4, 2017

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

VeenaVikraman
Community Advisor
Community Advisor
December 4, 2017

Hi Scott.

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

manoj_devapath
Level 5
December 5, 2017

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)

Arun_Chola
Level 2
December 5, 2017

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

smacdonald2008
Level 10
December 5, 2017

We have a discussion on how to get a TagManager API object in a HTL Java component here -- Adobe Experience Manager Help | Developing HTML Template Language Components that search for Adobe Experience Manager Co…

VeenaVikraman
Community Advisor
Community Advisor
December 5, 2017

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

Arun_Chola
Level 2
December 6, 2017

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.