How to add multivalue property to jcr node through java code?

gstackoverflow picture gstackoverflow · Nov 25, 2014 · Viewed 7k times · Source

According this answer

https://stackoverflow.com/a/18726682/2674303

I see that I can add property to node in crxde. But I don't understand how can I add multivalue property(array) to node.

Please, help.

Answer

Tomek Rękawek picture Tomek Rękawek · Nov 25, 2014

You have to create an array of values:

ValueFactory valueFactory = session.getValueFactory();
Node node = session.getNode("/content/path/to/my/node");
Value[] values = new Value[3];
values[0] = valueFactory.createValue("First value");
values[1]  = valueFactory.createValue("Second value");
values[2] = valueFactory.createValue("Third value");
node.setProperty("propertyName", values);

alternatively, you may use a String array:

node.setProperty("propertyName", new String[] {"First value", "Second value", "Third value"});