Expand my Community achievements bar.

SOLVED

set the property for single value string array data type using java

Avatar

Former Community Member

Hi,

How to set the property for single value string array data type using java in AEM.

--------------------------------------------------------

eg: AEM  Properties

Name:color

Type:String[]

Value: ["yellow"]

-------------------------------------------------------

Java code:

content.setProperty(paraname, paramvalue[0]);

------------------------------------------------------

 

 

Please provide the solutions

 

 

Thanks in advance,

1 Accepted Solution

Avatar

Correct answer by
Level 10

You can set a property as an Array the same way as other properties using the JCR API. You invoke the Node object;s setProperty method. 

Consider the following code:

 Node node = session.getNode("/content/dam/reports");

 javax.jcr.ValueFactory valueFactory = session.getValueFactory();

Node nodereport = session.getNode("/content/reports");

String[] colors ={"Red","Black","Orange","Blue"};

nodereport.setProperty("colors",colors);

 

When you run this code - you add a new property named colors to the /content/reports node -- as shown here:

[img]report.png[/img]

If you want to set a single value of the array -- you can do that too:

  nodereport.setProperty("colorssingle",colors[0]);

In this case -- singlecolor is assigned Red - and the data type of colorssingle is String -- not String[].

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

You can set a property as an Array the same way as other properties using the JCR API. You invoke the Node object;s setProperty method. 

Consider the following code:

 Node node = session.getNode("/content/dam/reports");

 javax.jcr.ValueFactory valueFactory = session.getValueFactory();

Node nodereport = session.getNode("/content/reports");

String[] colors ={"Red","Black","Orange","Blue"};

nodereport.setProperty("colors",colors);

 

When you run this code - you add a new property named colors to the /content/reports node -- as shown here:

[img]report.png[/img]

If you want to set a single value of the array -- you can do that too:

  nodereport.setProperty("colorssingle",colors[0]);

In this case -- singlecolor is assigned Red - and the data type of colorssingle is String -- not String[].