Getting Projection Objects Using the SCSM SDK ObjectProjectionCriteria Class

Getting Projection Objects Using the SCSM SDK ObjectProjectionCriteria Class

  • Comments 3
  • Likes

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();
        }
    }
}

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Please Help

    I am trying to submit asp form user input to the the service request, but I am getting errors

    USER INPUT is not the property of System.WorkItem.ServiceRequest.Library or System.WorkItem.

    ManagementPack mpserviceRequest = enterpriseManagementGRoup.ManagementPacks.GetManagementPack("System.WorkItem.ServiceRequest.Library", "31bf3856ad364e35", null);

    ManagementPackClass changeClass = enterpriseManagementGRoup.EntityTypes.GetClass("System.WorkItem", mpSystemWorkItemLibrary);

    ManagementPackClass serviceRequestClass = mpserviceRequest.GetClass("System.WorkItem.ServiceRequest");

    EnterpriseManagementObjectProjection proj = new EnterpriseManagementObjectProjection(enterpriseManagementGRoup, serviceRequestClass);

    proj.Object[serviceRequestClass, "User Input"].Value =String.Format(@"<UserInputs>

                            <UserInput Question =""Requestor Name"" Answer=" + TxtName.Text +@"/>

                             <UserInput Question =""Server Name"" Answer=" + TxtEmail.Text + @"/>

                             </UserInputs>");

  • @paulchals -

    Looks like you have a space between 'User' and 'Input' here:

    proj.Object[serviceRequestClass, "User Input"].Value =String.Format(@"<UserInputs>

                           <UserInput Question =""Requestor Name"" Answer=" + TxtName.Text +@"/>

                            <UserInput Question =""Server Name"" Answer=" + TxtEmail.Text + @"/>

                            </UserInputs>");

    Property names never have spaces in them.  I'm pretty sure this should be 'UserInput'.

  • Thank you Travis. It worked for me.

    I was able to post the user request to create the service request, but the RunBook associated to the SR is not mapping to the user request.