KBEC-00097 - Using JavaScript property expansion to iterate through available properties

Summary

Occasionally, you may need to list all available properties in a property sheet or associated with an intrinsic (/my…).

A couple of use cases for this:

  • You do not know the name of a property and do not have access to XPath
  • You are retrieving the values of properties stored in a property sheet in a previous step.

Solution

Use a JavaScript expansion and a ‘for’ loop to get the properties.

$[/javascript
for (var propName in ) {
    // do something
}
]


Example 1 - list all properties on an object

The following JavaScript will echo all of the properties of the /myWorkspace property sheet in a command shell.

$[/javascript
var props = "echo all props:";
for (var propName in myWorkspace) {
    var props = props + "\necho " + propName + " = " + myWorkspace[propName];
}
props;
]


Example 2 - list all properties on a property sheet

The following JavaScript will echo all of the properties on a property sheet in a command shell.

$[/javascript
var props = ""; // Initialize output variable
for (var propName in myProject.anyPropertysheet.properties) {
    var props = props + "echo " + propName + " = " + myProject.anyPropertysheet.properties[propName].value + "\n";
} // Populate variable with list of echo commands
props; // Printout list of commands
]


Example 3 - recursively list all custom properties on a property sheet

The following JavaScript will show an entire hierarchy of properties and property sheets.

$[/javascript
// Define a recursive function that will show an entire hierarchy of
//   properties and property sheets
// To start, you can pass any object or property sheet
//
// It only displays properties from the "custom property sheet" of
//   an object, not its intrinsic properties

 function showProperties(ecObject)
  {
      var retVal = '';

      // Use "properties" to get a list of all properties in an object
      for (var propertyName in ecObject.properties) {

            // Get the property (which may be a property sheet)
            var currentProperty = ecObject.properties[propertyName];

            // If the property has .value defined, it's a value.
            if (typeof (currentProperty.value) != 'undefined') {
                retVal += propertyName + ' - ' + currentProperty.value + '\n';
            } else {
                // It is a nested sheet - Display the sheet via recursive call
                retVal += '*** Property Sheet ' + propertyName + ' ***\n';
                retVal += showProperties(currentProperty);
                retVal += '*** End of ' + propertyName + ' ***\n';
            }
      }
      return (retVal);
  }


  // Sample calls
  var str = '';
  startingObjectOrSheet = server.settings;
  str += showProperties(startingObjectOrSheet)
  var startingObjectOrSheet = projects['Electric Cloud'].schedules.SentryMonitor;
  str += showProperties(startingObjectOrSheet)
  str;
]


Have more questions?

0 Comments

Please sign in to leave a comment.