Querying For Objects Using Object Criteria – Object Query Criteria Tool Example

Querying For Objects Using Object Criteria – Object Query Criteria Tool Example

  • Comments 4
  • Likes

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:

http://blogs.technet.com/servicemanager/archive/2009/12/30/how-to-write-a-custom-connector-csv-connector-example.aspx

http://blogs.technet.com/servicemanager/archive/2010/01/04/creating-a-custom-administration-setting.aspx

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

using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

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))
{
    //Do something
}

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.

image

 

image

You can get more fancy with the criteria by doing things like:

  1. Querying over related objects using TypeProjections
  2. And/Or different criteria together
  3. Use different operators
  4. etc.

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:

http://cid-17faa48294add53f.skydrive.live.com/self.aspx/.Public/Tools/ObjectQueryCriteriaTool.zip

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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great post, helped me alot.

    However, I´m stuck trying to get the Items with a certain affected user for the workitem.incident. What would such a query look like?

  • Querying for objects using related objects as criteria requires using type projections.  The ObjectQueryCriteria Tool currently doesnt suppor that.  I plan to add support to do that soon though.  In the meantime I think there are might be some examples here:

    blogs.technet.com/.../using-the-sdk-to-create-and-edit-objects-and-relationships-using-type-projections.aspx

  • Ok, thanks, but what would such a query look like?

    I´ve already read that post and yes that will work, however I don´t want to iterate through the result. Solving it with a query must be much more efficient. Right?

    I have a parameter and that´s the username (affected user), and as a result I want the Incident ID and Title for the related Incidents.

    Thanks in advance...

  • Hello Travis!

    This tool is exactly what i am looking for right now for SCSM2012.

    but the files on skydrive is not there anymore.

    Any chance you can upload them again?

    (if it does not work for SCSM2012, i can fix it myself and publish the code if you want)

    Best Regards

    Jakob