Skip navigation

CQ5

phneville
Currently Being Moderated

Programmatically search for a page via a property value

May 3, 2012 1:43 AM

Tags: #search #property

Hi,

 

I really need help with this, using java I need to search for a page via property value.

 

Thanks.

 
Replies
  • Currently Being Moderated
    May 3, 2012 6:20 AM   in reply to phneville

    I don't know if this performs well, but this snippet traverses a branch with root "node", finds jcr:content nodes, and gets a specific child node. You could use the node.getproperty() method in your case and traverse the /content branch (or wherever your pages are stored). See the javax.jcr.node (http://www.day.com/maven/jsr170/javadocs/jcr-1.0/javax/jcr/Node.html). Also see org.apache.sling.jcr.api.SlingRepository (http://dev.day.com/docs/en/cq/current/javadoc/index.html)

     

    NodeIterator iter = node.getNodes();

    while (iter.hasNext()) {

            Node n = iter.nextNode();

           //Look for "jcr:content" nodes

           if (n.getName().equals("jcr:content")) {

                   //get the path of the model node and save it

                   if(n.hasNode(MODEL_NODE)){

                         modelIds.add(n.getNode(MODEL_NODE).getPath());

                   }

           } else{

           //Scan child nodes

           getModelIds(n);

          }

                           }

     

     

    scott

     
    |
    Mark as:
  • Justin Edelson
    252 posts
    Nov 24, 2010
    Currently Being Moderated
    May 3, 2012 6:34 AM   in reply to phneville

    You can also use JCR Query for this. As an example, to search for all pages whose cq:template property is /apps/geometrixx-outdoors/templates/page_sidebar, you could use this XPath query:

    //element(*, cq:PageContent)[cq:template='/apps/geometrixx-outdoors/templates/page _sidebar']

     
    |
    Mark as:
  • Currently Being Moderated
    May 3, 2012 8:28 AM   in reply to Justin Edelson

    Since CQ 5.4 XPath is deprecated for queries.

    To execute an SQL2 query for all pages where the cq:template property contains the value '/apps/geometrixx-outdoors/templates/page_sidebar' you need the following code:

     

    final String queryString = "SELECT pc.* FROM [cq:PageContent] where [cq:template] = '/apps/geometrixx-outdoors/templates/page_sidebar'";

    final QueryManager qm = getSession().getWorkspace().getQueryManager();

    final Query query = qm.createQuery(queryString, Query.JCR_SQL2);

     

    For more information regarding JCR queries see the documentation for the javax.jcr.query package.

     
    |
    Mark as:
  • Justin Edelson
    252 posts
    Nov 24, 2010
    Currently Being Moderated
    May 3, 2012 8:44 AM   in reply to Matthias Schoger

    > Since CQ 5.4 XPath is deprecated for queries.

    This is not true. XPath is deprecated in the JCR 2.0 spec, not in CQ 5.4

     
    |
    Mark as:
  • Currently Being Moderated
    May 3, 2012 8:46 AM   in reply to phneville

    Hi Matthias,

     

    this is another subject, but Xpath is only deprecated from JCR 2.0 specifications, not jackrabbit, nor CRX, nor CQ. You don't need to specify N querying languages, just one human and one programmatic.


    Then you can create/use as many languages as you want (there is also querybuilder for example).

     

    If you like xpath, use it, it is NOT deprecated.

     
    |
    Mark as:
  • Currently Being Moderated
    May 4, 2012 12:43 AM   in reply to nicolas peltier

    Hi Nicolas,

     

    this is interesting. If I create a query with language 'XPATH', I get a deprecation warning.

    But since the warning is from the javax.jcr class, that makes sense.

     

    Nevermind, thanks for the info, and nice to hear from you again.

     

    Best regards,

    Matthias

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 6:15 AM   in reply to phneville

    Hi

    You can use QueryBuilder API.For example :

    http://localhost:4502/bin/querybuilder.json?property=[property_name]&property.value=[property_value]&type=cq:Page

     

    For more information, please refer below link :

     

    http://dev.day.com/docs/en/cq/current/dam/customizing_and_extendingcq5 dam/query_builder.html

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points