Share via


Using SDK Criteria for Querying Types

In the previous post, we talked about using criteria to query on instances. This post talks about using criteria to query types in the management pack such as classes, relationships, tasks, views etc. These are queries such as:

· Query for all classes that match a given class name.

· Query for all relationships with a particular target class.

· Query for all tasks with a particular category.

· Query for all management packs of a particular version and so on.

There is a criteria class associated with each of these types. Example, the class ManagementPackClassCriteria is used to query for management pack classes.

 

namespace Microsoft.EnterpriseManagement.Configuration

public class ManagementPackClassCriteria : QueryCriteria<ManagementPackClassCriteria>

    {

       

    }

The following table indicates all the properties of a management pack class that can be queried for. Example – All classes with a specific management pack id, classes that are abstract, classes that are sealed etc.

Id

Name

Base

ManagementPackId

Hosted

Abstract

Extension

Accessibility

Singleton

Sealed

TimeAdded

LastModified

DisplayName

Description

Category

 

The constructor for these classes accepts a criteria xml as shown below:

public ManagementPackCriteria(string criteria)

{

}

All type related criteria classes provide a similar constructor. The criteria parameter follows the same schema as discussed in this post except for the following differences:

· Only <GenericProperty> tags are used and not <Property> for indicating properties to query. As a result the syntax followed is <GenericProperty>propertyName</GenericProperty>

 

· ContainmentCriteriaType and FullTextCriteriaType cannot be used.

 

· Guid values for types need to be used instead of type names as there is no managementpack parameter exposed and hence the type name cannot be resolved.

 

Here’s an example of a criteria xml that queries for all classes in a given management pack.

<Criteria xmlns="https://Microsoft.EnterpriseManagement.Core.Criteria/">

  <Expression>

    <SimpleExpression>

      <ValueExpressionLeft>

        <GenericProperty>ManagementPackId</GenericProperty>

      </ValueExpressionLeft>

      <Operator>Equal</Operator>

      <ValueExpressionRight>

        <Value>dbb151b7-53e0-4de0-967e-a1ade70f5c3c</Value>

      </ValueExpressionRight>

    </SimpleExpression>

  </Expression>

</Criteria>

 

The code for the same looks as below:

ManagementPackClassCriteria criteria = new ManagementPackClassCriteria(criteria);

IList<ManagementPackClass> mpClasses = managementGroup.EntityTypes.GetClasses(criteria);

 

In this post, we talked about querying for all incidents that were related to config items. Consider querying for all relationships that exist in the system that include config item as a source or target. The criteria xml for the same would look like:

 

 

<Criteria xmlns="https://Microsoft.EnterpriseManagement.Core.Criteria/">

  <Expression>

    <Or>

      <Expression>

        <SimpleExpression>

          <ValueExpressionLeft>

            <GenericProperty>SourceType</GenericProperty>

          </ValueExpressionLeft>

          <Operator>Equal</Operator>

          <ValueExpressionRight>

            <Value>replace with guid value for config item type</Value>

          </ValueExpressionRight>

        </SimpleExpression>

      </Expression>

      <Expression>

        <SimpleExpression>

          <ValueExpressionLeft>

            <GenericProperty>TargetType</GenericProperty>

          </ValueExpressionLeft>

          <Operator>Equal</Operator>

          <ValueExpressionRight>

            <Value>replace with guid value for config item type</Value>

          </ValueExpressionRight>

        </SimpleExpression>

      </Expression>

    </Or>

  </Expression>

</Criteria>

 

The following table indicates all the properties on which a ManagementPackRelationship can be queried for

 

 

Name

Base

SourceType

TargetType

ManagementPackId

Abstract

Accessibility

Sealed

TimeAdded

LastModified

DisplayName

Description

Category

 

 

Note: For operations that involve querying for classes or relationships frequently, it will be better to use regular APIs without criteria and compare results on the client for performance reasons.