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.
Thanks for the example - using this example what code could I use to set the affected user, and the source of the incident?
There are two ways to create/set relationships:
1) Using type projections - more information here:
blogs.msdn.com/.../getting-started-with-type-projections.aspx
blogs.msdn.com/.../more-with-type-projections.aspx
blogs.msdn.com/.../getting-and-working-with-type-projections-basic.aspx
I'll also be doing a big blog post on working programmatically with type projections on this blog soon. Watch for that.
2) Creating relationships manually - do this by getting the relationship object, the source object and the target object. Then set the source and target property of the relationship object and .Submit() it.
To set the Source of the incident you would do the same as for Impact or Urgency above. Get the correct enumeration value and set the projectionIncident.Object[classIncident, "Source"].Value = someSource.Id
Hi there,
I tried to execute the code on our Service Manager server. I am getting the following error:
Microsoft.EnterpriseManagement.Common.ObjectNotFoundException: An object of class ManagementPack with name System.WorkItem.Incident.Library was not found.
I even tried replacing 'localhost' with FQDN Server Name in line
still no luck.
System Center Data Access Service is running on Service Manager Server.
Service Manager Console is working properly.
Why can't the above code connect to the Data Access Service?
Where can i start looking for bugs?
thanks,
Aleem
BTW,
I am running System Center 2012 Service Manager Service Pack 1.
@Aleem -
This error message you are getting is not about a failed connection. It is telling you that there is no MP with the identity of Microsoft.WorkItem.Incident.Library with the same public key token and version that you are passing in. This is because the code that I wrote here is not particularly good. It assumes that the Microsoft.WorkItem.Incident.Library version would always be the same as the system library MP version. Since the time of writing this blog post the version numbers have become different.
Thanks for the input Travis.
I was able to pull "ConnectedUser" names from my ServiceManager server yesterday. I notice the version and keytoken you were using for "System.WorkItem.Incident.Library" are of SystemManagementPack.System.
My question is what are the best practices to call GetManagementPack to get (lets say) a list of all ServiceReuqests in SM CMDB?
any url link will be helpful.
FYI, (just sharing for others)
I was able to follow sample code in this link and made good progress.
"Create Service Request from Request Offer meet Exception"
social.technet.microsoft.com/.../461a4e29-2f77-4702-a008-3ab105bf1afe