Read a property of an arbitrary CmsResource

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(missing parentheses)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
With scriptlets, you can read the properties of any opencms resource.  
 
With scriptlets, you can read the properties of any opencms resource.  
  
 
+
<source lang="java">
 
   <%@page import="org.opencms.file.CmsProperty"%>
 
   <%@page import="org.opencms.file.CmsProperty"%>
 
   <%
 
   <%
       CmsObject cms;
+
       CmsJspActionElement cmsAction    = new CmsJspActionElement (pageContext, request, response);
     
+
       CmsObject cmsObject              = cmsAction.getCmsObject();
      // .. get the cms object
+
     
+
      boolean search = false;
+
       CmsProperty prop = cms.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);
+
 
        
 
        
 +
      // Try to find the "prop_name" property of the current file and, if it is not found there, try to find it on all parent folders:
 +
      boolean search = true;
 +
      CmsProperty prop = cms.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);     
 
       String propValue = (prop != null) ? prop.getValue() : null;
 
       String propValue = (prop != null) ? prop.getValue() : null;
 +
 +
    // ... or get the title propery ...
 +
    CmsProperty titleProp = cmsObject.readPropertyObject (cmsAction.getRequestContext().getUri(), "Title", false);
 +
    String title = (titleProp != null) ? titleProp.getValue() : null;
 
   %>
 
   %>
 +
</source>
 +
 +
[[Category:Developing in OpenCms]]

Latest revision as of 18:00, 9 April 2015

With scriptlets, you can read the properties of any opencms resource.

   <%@page import="org.opencms.file.CmsProperty"%>
   <%
      CmsJspActionElement cmsAction    = new CmsJspActionElement (pageContext, request, response);
      CmsObject cmsObject              = cmsAction.getCmsObject();
 
      // Try to find the "prop_name" property of the current file and, if it is not found there, try to find it on all parent folders:
      boolean search = true;
      CmsProperty prop = cms.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);      
      String propValue = (prop != null) ? prop.getValue() : null;
 
     // ... or get the title propery ...
     CmsProperty titleProp = cmsObject.readPropertyObject (cmsAction.getRequestContext().getUri(), "Title", false);
     String title = (titleProp != null) ? titleProp.getValue() : null;
   %>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox