The System Center Platform in Service Manager Part 3: The Data Access Service – Try It!
Published Feb 14 2019 09:35 PM 267 Views
First published on TECHNET on Aug 09, 2009

This post is a continuation in the series which describes the System Center common platform components implemented in Service Manager.  Previous posts:


The System Center Platform in Service Manager Part 1: Introduction


The System Center Platform in Service Manager Part 2: The Model-Based Database


The System Center Platform in Service Manager Part 2: The Model-Based Database - Try It!


The System Center Platform in Service Manager Part 3: The Data Access Service


In this post we’ll create a very simple command line application that will create a new incident in Service Manager by communicating with the Data Access Service.



You can either create a new C# console application using Visual Studio and copy paste this code into it or you can grab the compiled version that is attached to this post.  If you grab the compiled version it will only run on your Service Manager Management Server.  If you create a new console application you can either build it as and run it on your Management Server or change the server name as indicated in the code comments and run it remotely.



In order to compile this project you’ll need to add a reference to the Microsoft.EnterpriseManagement.Core.dll which you can find in the SDK Binaries directory on the Management Server where you installed the product (default: C:\Program Files\Microsoft System Center\Service Manager 2010\SDK Binaries)



NOTE: This sample code has only been tested on Beta 2 builds of Service Manager.  It may or may not work on Beta 1 or CTP2.  Beta 2 is not publicly released yet.  If this doesn’t work for you on Beta 1 or CTP2 (TAP release only), please try again when Beta 2 comes out.


Here we go!



using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


using System.IO;


using Microsoft.EnterpriseManagement;


using Microsoft.EnterpriseManagement.Common;


using Microsoft.EnterpriseManagement.Configuration;



namespace CreateIncident


{


class Program


{


static void Main(string[] args)


{


try


{


//First, create a connection to the management group.  Replace 'localhost' with your server name if you want to run remotely.


EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");



//Next, you'll need the version and keytoken of the system management packs. This is an easy way to do it.


ManagementPack mpSystem = mg.ManagementPacks.GetManagementPack(SystemManagementPack.System);


Version version = mpSystem.Version;


string keytoken = mpSystem.KeyToken;



//Next, get the incident library management pack by ID - System.WorkItem.Incident.Library


ManagementPack mpIncidentLibrary = mg.GetManagementPack("System.WorkItem.Incident.Library", keytoken, version);



// Next, get the incident class.


ManagementPackClass classIncident = mg.EntityTypes.GetClass("System.WorkItem.Incident", mpIncidentLibrary);



// Next, create a new object projection.


EnterpriseManagementObjectProjection projectionIncident = new EnterpriseManagementObjectProjection(mg, classIncident);



// Next, set the Title property on the incident.


projectionIncident.Object[classIncident, "Title"].Value = "This is a test";



// Next, you'll need to get the Urgency enumeration, and then get the High enumeration value, and finally set it on the incident.


// Note: this method of looking at the property type.GetManagementPack() is a surefire way to make sure you get the right MP.


ManagementPack mpIncidentUrgencyEnum = projectionIncident.Object[classIncident, "Urgency"].Type.GetManagementPack();


ManagementPackEnumeration enumUrgencyHigh = mpIncidentUrgencyEnum.GetEnumeration("System.WorkItem.TroubleTicket.UrgencyEnum.High");


projectionIncident.Object[classIncident, "Urgency"].Value = enumUrgencyHigh.Id;



// Next, do the same for the Impact enum.


ManagementPack mpIncidentImpactEnum = projectionIncident.Object[classIncident, "Urgency"].Type.GetManagementPack();


ManagementPackEnumeration enumImpactHigh = mpIncidentUrgencyEnum.GetEnumeration("System.WorkItem.TroubleTicket.ImpactEnum.High");


projectionIncident.Object[classIncident, "Impact"].Value = enumImpactHigh.Id;



//Finally, commit the incident to the database.


projectionIncident.Commit();



}


catch (Exception e)


{


Console.Write(e.ToString());


}


}


}


}



There!  Now you have a handy utility that you can use to create incidents from the command line.  You could further evolve this to be parameterized so you could pass in the title, impact, urgency, etc. on the command line.


Check out the SDK documentation that is available with each release of the product (on the Connect site during the pre-release period).  The .chm file has a complete reference to the SDK and some examples of how to perform common tasks using the Data Access Service.


In the next post in this series, we’ll learn about the System Center Management Service .


CreateIncident.zip

Version history
Last update:
‎Mar 11 2019 08:14 AM
Updated by: