Getting Projection Objects Using the SCSM SDK ObjectProjectionCriteria Class

Quick sample here.  Let’s say you want to get all the service requests assigned to a particular support group (enum data type property).  No problem!  Use ObjectProjectionCriteria.  That will get you projection objects.  If you want to just get objects you can use the related EnterpriseManagementObjectCriteria class.  (what is a type projection/projection objects?)

image

Here are some more details:

http://blogs.msdn.com/b/scplat/archive/2010/12/27/using-sdk-criteria-querying-instances.aspx

 

Here are some examples that people have put out on the web.

http://scsmnz.net/c-code-snippets-for-service-manager-1/

http://scsmnz.net/c-code-snippets-for-service-manager-2/

http://blog.scsmsolutions.com/2011/09/codesnipets-for-scsm-2010-sdk/#Get_the_class_by_ID_Guid_or_name

Party on!

 

Here’s the code for the project if you want to copy it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Common;

namespace QuerySRsBySupportGroup
{
    class Program
    {
         static void Main(string[] args)
        {
            //Create connection to Managment Server
            EnterpriseManagementGroup emg = new EnterpriseManagementGroup("localhost");
             //Get the SR Advanced type projection
            ManagementPackTypeProjectionCriteria mptpcServiceRequestAdvanced =
                new ManagementPackTypeProjectionCriteria("Name =’System.WorkItem.ServiceRequestProjection’");
            ManagementPackTypeProjection mptpServiceRequestAdvanced =
                 emg.EntityTypes.GetTypeProjections(mptpcServiceRequestAdvanced).First<ManagementPackTypeProjection>();
            //Get the SR Library MP
            ManagementPackCriteria mpcServiceRequestMP =
                new ManagementPackCriteria("Name = ‘System.WorkItem.ServiceRequest.Library’");
            ManagementPack mpServiceRequestLibrary =
                emg.ManagementPacks.GetManagementPacks(mpcServiceRequestMP).First<ManagementPack>();
            //Get a particular support group enumeration
            ManagementPackEnumerationCriteria mpecSupportGroup =
                new ManagementPackEnumerationCriteria("Name = ‘Enum.cd96b8a5444b41d081e658ee0a90383c’");
             ManagementPackEnumeration mpeSupportGroup =
                emg.EntityTypes.GetEnumerations(mpecSupportGroup).First<ManagementPackEnumeration>();

            //Create the criteria
             string strCriteria =
                String.Format(
                 @"<Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">
                    <Expression>
                         <SimpleExpression>
                             <ValueExpressionLeft>
                                <Property>$Context/Property[Type=’System.WorkItem.ServiceRequest’]/SupportGroup$</Property>
                            </ValueExpressionLeft>
                            <Operator>Equal</Operator>
                            < ValueExpressionRight>
                                 <Value>{0}</Value>
                             </ValueExpressionRight>
                        </SimpleExpression>
                    </Expression>
                < /Criteria>", mpeSupportGroup.Id.ToString());

            //Create the ObjectProjectionCriteria.  Include the MP (mpServiceRequestLibrary) that the type projection seed class is contained in.
            ObjectProjectionCriteria opc =
                new ObjectProjectionCriteria(strCriteria, mptpServiceRequestAdvanced,mpServiceRequestLibrary, emg);
            //Get the SRs
            IObjectProjectionReader<EnterpriseManagementObject> oprServiceRequests =
                emg.EntityObjects.GetObjectProjectionReader<EnterpriseManagementObject>(opc, ObjectQueryOptions.Default);
            //Do whatever you want with them…
            foreach (EnterpriseManagementObjectProjection emopServiceRequest in oprServiceRequests)
            {
                 Console.WriteLine(emopServiceRequest.Object.Id);
             }
            Console.ReadLine();
        }
    }
}