How to Query for Instances
Applies To: System Center 2012 - Service Manager
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
The EnterpriseManagementGroup object that is used to connect to the server, has a property named EntityObjects. EntityObjects let you to read multiple objects from Service Manager by using the GetObjectReader method. GetObjectReader provides overloads that can get enterprise management objects by using a list of identifiers, the management pack class type of the objects, or by querying through criteria.
There may be occasions where you do not want to just return all instances of a specific ManagementPackClass. By using criteria with your object query, you can filter out instances that you do not want. Criteria can be thought of a set of rules that are applied to each object in the query. If the rules do not pass, the object is not included on the result set returned from the query. For more information, see Instance Manipulation.
To query for instances of a management pack class using criteria
Connect to the server by using the EnterpriseManagementGroup class.
Obtain reference to the ManagementPackClass definition that you want to obtain all instances of.
Create an instance of the EnterpriseManagementObjectCriteria object. On the constructor, pass the criteria string that is used for filtering instances.
Call the method GetObjectReader implemented by the EntityObjects property. Provide the EnterpriseManagementObjectCriteria from the previous step.
The result of the previous step will be a IObjectReader object with all the results and can be enumerated.
Example
The following example obtains a management pack class from a management pack. That management pack class is used to build a criteria object that retrieves each RePackaging.Request object that matches a specific SoftwareTitle (a property of the management pack class) value. The identifiers of the instances are added to a List object:
EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");
ManagementPack mp = mg.GetManagementPack("RePackaging.Library", null, new Version());
ManagementPackClass requestClass = mp.GetClass("RePackaging.Request");
IObjectReader<EnterpriseManagementObject> reader;
EnterpriseManagementObjectCriteria criteria;
List<Guid> objectIds;
criteria = new EnterpriseManagementObjectCriteria("SoftwareTitle='Software Application A'", requestClass);
reader = mg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(criteria, ObjectQueryOptions.Default);
objectIds = new List<Guid>(reader.MaxCount);
foreach (var item in reader)
{
objectIds.Add(item.Id);
}
Compiling the Code
Namespaces
System |
System.Collections.Generic |
Microsoft.EnterpriseManagement |
Microsoft.EnterpriseManagement.Common |
Microsoft.EnterpriseManagement.Configuration |
Assemblies
mscorlib |
Microsoft.EnterpriseManagement.Core |
See Also
Tasks
How to Create an Instance
How to Get a Single Instance
How to Query for Instances
How to Delete Instances
Reference
EnterpriseManagementGroup
EntityObjects
GetObjectReader
IObjectReader
ManagementPackClass