First published on TECHNET on Apr 26, 2010
Because Service Manager is an extensible model-based system, the API layer for Service Manager is intentionally very generic. There isnt a method on the API layer called ‘GetIncident()’ for example. Nor is there a ‘GetComputer()’, ‘UpdateComputer()'’, etc. We have generic APIs for things like ‘GetObject()’ which will get you incidents, computers, change requests, hard drives, or whatever you want depending on the criteria you provide the API call. In pseudo-code I would call something like ‘GetObject(Incident, ID=1234)’ instead of calling ‘GetIncident(ID=1234)’. Make sense?
OK – so now the question is A) What API do I call? and B) How do I formulate my criteria?
There are several APIs that you can call and several overload methods on those to get objects. You’ve seen some examples of these in other blogs that I’ve written like these:
In this particular blog post I will show you how to use the GetObjectReader() API to get a Collection of EnterpriseManagementObjects that match the criteria specified. This is what the code looks like at a high level:
//First add a reference in your project to Microsoft.EntperpriseManagement.Core.dll
EnterpriseManagementGroup emg = new EnterpriseManagementGroup(“some server name”);
Version mpVersion = new Version(“some version number”);
ManagementPack mp = emg.GetManagementPack(“some MP ID”, “some public key token”, mpVersion);
ManagementPackClass mpClass = emg.EntityTypes.GetClass(“some class ID”, mp);
EnterpriseManagementObjectCriteria emoCriteria = new EnterpriseManagementObjectCriteria(“some query criteria XML”, mpClass, emg);
foreach (EnterpriseManagementObject emo in emg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(emoCriteria, ObjectQueryOptions.Default))
So – basically we provide a MP ID, Version, Public Key Token of the class of objects that we want to query for. This will get us the ManagementPackClass. Then we pass that along with some criteria XML to the method GetObjectReader.
The criteria XML can be a bit tricky so I’ve built a handy little tool for you to test out different variations of the criteria XML so you can get the desired syntax and query criteria right for your scenario. Below are some screenshots of the tool. The first one gets all objects which have an ‘a’ in the DisplayName. The second one looks for Work Items where the ID is exactly ‘RA602’. Notice the difference in the Operator element – in the first case it is ‘Like’ and I used SQL wildcards in the Value element to search for objects. In the second case, I used ‘Equals’ in the Operator element so that it searched for an exact match.
You can get more fancy with the criteria by doing things like:
Querying over related objects using TypeProjections
And/Or different criteria together
Use different operators
I’ll provide some more complex examples in the future, but feel free to leave your examples in the comments below to help out others.
The tool can be downloaded from here:
I’ve included the full Visual Studio project with source if you want to modify it or see how I wrote the query code. I’ve also included a couple of XML files with query criteria samples.