Thursday, August 30, 2012

SCSM Creating Manual Activity for Incident in C#


Some time ago I had to develope application that could connect two systems: Microsoft Dynamics CRM & SCSM (Microsoft System Center Service Manager). The main purpose of this application is to synchronize data between two applications.

In this article i will show you only some little part (but maybe one of the  toughest for me...) of my application code.
I will continue to post some other usefull parts from this project. 

The code consists of 3 main steps:

1. Create relationship between incident & manual activity
2. Create manual activity
3. Assign created activity to incident

There is good post about creating incident in SCSM which really helped me out: http://blogs.technet.com/b/servicemanager/archive/2009/08/09/the-system-center-platform-in-service-manager-part-3-the-data-access-service-try-it.aspx

//Create relationship between incident & manual acticvity
ManagementPackRelationship workItemContainsActivityRelationship =
mg.EntityTypes.GetRelationshipClass(new Guid("2DA498BE-0485-B2B2-D520-6EBD1698E61B"));
                                             
CreatableEnterpriseManagementRelationshipObject AssignedToIncident = 

new CreatableEnterpriseManagementRelationshipObject(mg, workItemContainsActivityRelationship);

//Create Manual Activity
CreatableEnterpriseManagementObject activityItemObject = 

new CreatableEnterpriseManagementObject(mg, classManualActivity);
Guid newActivityGuid = Guid.NewGuid();
                                             
activityItemObject[classManualActivity, "Id"].Value = newActivityGuid.ToString();                                              
activityItemObject[classManualActivity, "Title"].Value = "<Title>";
activityItemObject[classManualActivity, "Description"].Value = "<Description>";

                                             
EnterpriseManagementObjectGenericCriteria genericCr = 

new EnterpriseManagementObjectGenericCriteria("Id == '" + <SCSM Incident Id>.ToString() + "'");
IObjectReader<EnterpriseManagementObject> reader = mg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(genericCr, ObjectQueryOptions.Default);


 if (reader != null && reader.Count > 0) {
foreach (EnterpriseManagementObject obj in reader) {

AssignedToIncident.SetSource(obj);
AssignedToIncident.SetTarget(activityItemObject);

IncrementalDiscoveryData dd = new IncrementalDiscoveryData();
dd.Add(activityItemObject);
dd.Add(AssignedToIncident);
dd.Overwrite(mg);

//Assign Manual Activity to Incident
AssignedToIncident.Commit();
         }
}

2 comments:

  1. Incomplete code for creation classManualActivity, please update!!!

    ReplyDelete
  2. Hi,

    I know this is an old post but...

    I have tried this script working with Service requests but it fails at the point of adding the SR ID. All I can seem to bring back is the friendly ID (SR123456) as opposed to the GUID that it seems to require. For the life of me I cant work out in C# how to get this value.

    In powershell I can find the object and use $object.get_id()...

    or as in my case I am writing a script to run from an activity which will add another activity to the SR I can look at the relationship already created (system.workitemcontainsactivity) and get it from the $relationship.sourceobject.ID ... but once more this is in powershell, not in C#

    Any clues on how to get the GUID of an object in C# would be greatly received!

    Cheers Ed

    ReplyDelete