5 Replies Latest reply on Nov 30, 2012 12:26 AM by Rajesh Pandian

    how to add values to multi valued properties using JSP

    Rajesh Pandian Level 1

      Hi,

       

      I am able to use a multi valued property in JSP which is populated manually using CRXDE. I am able to set the multi valued property only by completely replacing it, but not by appending values to it.

       

      Can you please help me to append values to the multi valued property?

       

      Thanks in advance,

      Rajesh.

        • 1. Re: how to add values to multi valued properties using JSP
          Veena_07 Level 3

          Hi Rajesh

           

               Can u explain a bit more on ur requirement. Am not clear on ur question

           

          Thanks

          Veena

          • 2. Re: how to add values to multi valued properties using JSP
            rush_pawan Level 4

            Hi,

             

            Please use "javax.jcr.Node" class to set property of any of the node in CQ. If you have resource or page object and just adapt it to Node and then you can set it. Example below

             

            Let say you have a page node (resource) and want to set a property with multi value

             

            Node pageNode=currentPage.getContentResource().adaptTo(Node.class);

            //Node resourceNode=resource.adaptTo(Node.class);

            String[] value={"first","second","third"};

            pageNode.setProperty("prop1",value);

            pageNode.save();

             

            Above example is for string[] but you can set different types as well. Please refer below API

            http://www.day.com/maven/jsr170/javadocs/jcr-1.0/javax/jcr/Node.html

             

            I hope it helps you. Let me know for more information.

             

            Thanks,

            Pawan

            • 3. Re: how to add values to multi valued properties using JSP
              Rajesh Pandian Level 1

              We have a requirement where we need to append values to a Multi-valued property.

               

              We are able to replace the whole list of values present in the multi valued property using a value array in JSP through Node.setProperty().

               

              But we need to append values to the Multi valued property and not replace the whole content.

               

              For example, say we have a property named "Employee_Id" which is a multi valued property and has values [1001,1002,1003]

               

              Now, if we need to append a value, say 1004, to the multi valued property we have to get all the values of this property using Node.getProperty,

               

              List<Value> employeeIdList = new ArrayList<Value>();

               

              for(Value v : node.getProperty("Employee_Id").getValues()) {

                 if(v!=null){

                      employeeIdList.add(v);

                 }

              }

               

              Then we have to add the value "1004" to the employeeIdList before setting the Employee_Id property with this list of values.

               

              I need a way to append the values to the property directly without using an exrternal list to populate it.

               

              Can you please help?

              • 4. Re: how to add values to multi valued properties using JSP
                rush_pawan Level 4

                Hi,

                 

                Use this code. Taken an example of updating String data property (you can tweak as needed)

                 

                Node pageNode=currentPage.getContentResource().adaptTo(Node.class);

                Value vs[]=new Value[0];

                List<Value> employeeIdList=new ArrayList<Value>();

                for(Value v : pageNode.getProperty("mytest").getValues()) {

                   if(v!=null){

                        employeeIdList.add(v);

                   }

                }

                ValueFactory vFactory=pageNode.getSession().getValueFactory();

                Value newValue=vFactory.createValue("nextvalue");

                employeeIdList.add(newValue);

                Value[] finalValues=(Value[])employeeIdList.toArray(vs);

                pageNode.setProperty("mytest",finalValues);

                pageNode.save()

                 

                I hope this will help you. If any issue let me know.

                 

                Thanks,

                Pawan

                • 5. Re: how to add values to multi valued properties using JSP
                  Rajesh Pandian Level 1

                  Dude Awesome man.It worked as a charm. Keep up the good work Thanks a lot Bro \m/