Using ObjectQueryOptions – Configuring Object Retrieval from the CMDB, Part II
Using ObjectQueryOptions – Configuring Object Retrieval from the CMDB Part II
This is a continuation of my previous post on Microsoft.EnterpriseManagement.Common.ObjectQueryOptions. The previous post introduced the basics of object retrieval from the CMDB. In this post, I want to explain how you to get back results from the CMDB sorted in some order using ObjectQueryOptions.
Objects of a class or projection can be retrieved from the CMDB sorted by properties of the object.
There are two kinds of properties in every object. Those that are defined on a class in a management pack and those that are applicable to all objects for e.g LastModified, TimeAdded etc..
The properties applicable to all objects are called generic properties. The entire list of generic properties is below:
namespace: Microsoft.EnterpriseManagement.Configuration
public enum.EnterpriseManagementObjectGenericPropertyName
{
Id = 0,
Name = 1,
Path = 2,
FullName = 3,
DisplayName = 4,
LastModified = 5,
TimeAdded = 6,
LastModifiedBy = 7
}
Case 1: Sorting objects of a class
The relevant API on ObjectQueryOptions for sorting class objects
/// <summary>
/// Used for sorting isntances by property
/// </summary>
/// <param name="property">Property to sort on</param>
/// <param name="sortOrder">Sort Order</param>
public void AddSortProperty(ISortableProperty property, SortingOrder sortOrder)
The above API is used to specify the sorting on objects of a class retrieved from the CMDB.
The property parameter can of type
Microsoft.EnterpriseManagement.Configuration. ManagementPackProperty
Or
Microsoft.EnterpriseManagement.Configuration.EnterpriseManagementObjectGenericProperty
Let’s look at an example of retrieving all available Microsoft.Windows.Computer from the CMDB sorted by ascending order of property NetworkName (defined on class Microsoft.Windows.Computer) and ascending order of generic property TimeAdded
EnterpriseManagementGroup emg = new EnterpriseManagementGroup("sm-server");
ManagementPackClass mp = emg.ManagementPacks.GetManagementPack("Microsoft.Windows.Library", keyToken, new Version("7.5.1049.1");
ManagementPackClass computer = emg.EntityTypes.GetClass("Microsoft.Windows.Computer", mp);
ObjectQueryOptions queryOptions = new ObjectQueryOptions(ObjectPropertyRetrievalBehavior.All);
// sort based on NetworkName
ManagementPackProperty networkName = computer["NetworkName"];
queryOptions.AddSortProperty(networkName, SortingOrder.Ascending;
// sort based on LastModified
EnterpriseManagementObjectGenericProperty genericProperty =
new EnterpriseManagementObjectGenericProperty(EnterpriseManagementObjectGenericPropertyName.TimeAdded);
queryOptions.AddSortProperty(genericProperty, SortingOrder.Ascending);
IObjectReader<EnterpriseManagementObject> instanceReader = emg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(computer, queryOptions);
Case 2: Sorting objects of projection types.
The relevant APIs on ObjectQueryOptions for sorting projection objects
// <summary>
/// Projection sorting based on generic and non-generic properties
/// </summary>
/// <param name="sortXml">properties to sort on, specified in xml</param>
/// <param name="projection">the projection type</param>
/// <param name="managementPack">the management pack to be used for resolving the properties specified in the sort xml</param>
/// <param name="managementGroup">the management group</param>
public void AddSortProperty(string sortXml, ManagementPackTypeProjection projection, ManagementPack managementPack, EnterpriseManagementGroup managementGroup)
// <summary>
/// Projection sorting based on generic and non-generic properties
/// </summary>
/// <param name="sortXml">properties to sort on, specified in xml</param>
/// <param name="projection">the projection type</param>
/// <param name="managementGroup">the management group</param>
public void AddSortProperty(string sortXml, ManagementPackTypeProjection typeProjection, EnterpriseManagementGroup managementGroup)
Below is an example of sorting objects of type System.WorkItem.Incident.ProjectionType.
Let’s retrieve all incidents assigned to user “Peter”, by descending order of priority (property defined on class System.WorkItem.TroubleTicket) and ascending order of AffectedUser (defined on class System.Domain.User )name in each incident.
Here, is the criteria I used for getting incidents assigned to “Peter”
<Criteria xmlns="https://Microsoft.EnterpriseManagement.Core.Criteria/">
<Reference Id="System.WorkItem.Library" Version="7.5.1083.0" Alias="WorkItem" PublicKeyToken="9396306c2be7fcc4"/>
<Reference Id="System.Library" Version="7.5.1083.0" Alias="System" PublicKeyToken="9396306c2be7fcc4"/>
<Expression>
<SimpleExpression>
<ValueExpressionLeft>
<Property>$Context/Path[Relationship='WorkItem!System.WorkItemAssignedToUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$</Property>
</ValueExpressionLeft>
<Operator>Equal</Operator>
<ValueExpressionRight>
<Value>Peter</Value>
</ValueExpressionRight>
</SimpleExpression>
</Expression>
</Criteria>
Here is the sort xml I used:
<Sorting xmlns="https://Microsoft.EnterpriseManagement.Core.Sorting">
<Reference Id="System.WorkItem.Library" Version="7.5.1083.0" Alias="WorkItem" PublicKeyToken="9396306c2be7fcc4"/>
<Reference Id="System.Library" Version="7.5.1083.0" PublicKeyToken="9396306c2be7fcc4" Alias="System" />
<SortProperty SortOrder="Descending">$Context/Property[Type='WorkItem!System.WorkItem.TroubleTicket']/Priority$</SortProperty>
<SortProperty SortOrder="Ascending">$Context/Path[Relationship='WorkItem!System.WorkItemAffectedUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$</SortProperty>
</Sorting>
EnterpriseManagementGroup emg = new EnterpriseManagementGroup("sm-server");
ManagementPackClass mp = emg.ManagementPacks.GetManagementPack("ServiceManager.IncidentManagement.Library", keyToken, new Version("7.5.1083.0"));
ManagementPackTypeProjection typeProjection = emg.EntityTypes.GetTypeProjection("System.WorkItem.Incident.ProjectionType",mp);
ObjectProjectionCriteria criteria = new ObjectProjectionCriteria(Criteria.incident,typeProjection, emg);
ObjectQueryOptions opt = new ObjectQueryOptions();
opt.AddSortProperty(Criteria.sort,typeProjection, emg);
IObjectProjectionReader<EnterpriseManagementObject> p = emg.EntityObjects.GetObjectProjectionReader<EnterpriseManagementObject>(criteria, opt);