Setting value is multi values property through java code | Community
Skip to main content
Arun_Chola
Level 2
Dec 1, 2017 7:53 PM UTC
Solved

Setting value is multi values property through java code

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.
manoj_devapath
manoj_devapathAccepted solution
Level 5
Dec 1, 2017 8:25 PM UTC

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

Arun_Chola
Level 2
Dec 6, 2017 5:50 PM UTC

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.

Translate
VeenaVikraman
New Participant
Dec 5, 2017 6:23 PM UTC

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

Translate
smacdonald2008
Level 10
Dec 5, 2017 6:16 PM UTC

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…

Translate
Arun_Chola
Level 2
Dec 5, 2017 5:52 PM UTC

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

Translate
manoj_devapath
Level 5
Dec 5, 2017 1:44 PM UTC

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)

Translate
VeenaVikraman
New Participant
Dec 4, 2017 9:41 PM UTC

Hi Scott.

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

Translate
VeenaVikraman
New Participant
Dec 4, 2017 9:39 PM UTC

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

Translate
Arun_Chola
Level 2
Dec 4, 2017 8:27 PM UTC

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?

Translate
smacdonald2008
Level 10
Dec 1, 2017 9:12 PM UTC

That is correct - you need to use an array.

Translate
manoj_devapath
manoj_devapathAccepted solution
Level 5
Dec 1, 2017 8:25 PM UTC

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[])

Translate