private static void CreateWorkItemsFromTemplate(ManagementPackObjectTemplate templateWorkItem) { try { Console.WriteLine("Creating work item..."); EnterpriseManagementObjectProjection emopIncident = new EnterpriseManagementObjectProjection(emg, templateWorkItem); emopIncident.Commit(); if (dtWorkItemCreateLast != DateTime.MinValue) { TimeSpan tsWorkItemCreateInterval = DateTime.Now - dtWorkItemCreateLast; pcWorkItemCreateInterval.RawValue = (long)tsWorkItemCreateInterval.TotalSeconds; Console.WriteLine("WI Create Interval: " + tsWorkItemCreateInterval.TotalSeconds); Console.WriteLine("Target WI Interval: " + dRateInSeconds); } if (dtWorkItemCreateLast != null) { } dtWorkItemCreateLast = DateTime.Now; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.InnerException.Message); } }
//Add action log relationship to passed emo private bool AddActionLogEntry( EnterpriseManagementGroup emg, EnterpriseManagementObject emoIncident, string strTitle, string strDescription ) { try { //Get the System.WorkItem.Library mp ManagementPack mpWorkItemLibrary = emg.ManagementPacks.GetManagementPack(new Guid("405D5590-B45F-1C97-024F-24338290453E")); //Get the actionlog class ManagementPackClass typeActionLog = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.ActionLog", mpWorkItemLibrary); //Create a new action log entry CreatableEnterpriseManagementObject objectActionLog = new CreatableEnterpriseManagementObject(emg, typeActionLog); //Setup the action log entry objectActionLog[typeActionLog, "Id"].Value = Guid.NewGuid().ToString(); objectActionLog[typeActionLog, "Description"].Value = strDescription + "\n"; objectActionLog[typeActionLog, "Title"].Value = strTitle; objectActionLog[typeActionLog, "EnteredBy"].Value = UserPrincipal.Current.DisplayName; objectActionLog[typeActionLog, "EnteredDate"].Value = DateTime.Now.ToUniversalTime(); //Get the enumeration and relationship for the actionlog entry ManagementPackEnumeration enumActionLog = mpWorkItemLibrary.GetEnumerations().GetItem("System.WorkItem.ActionLogEnum.TaskExecuted"); objectActionLog[typeActionLog, "ActionType"].Value = enumActionLog; ManagementPackRelationship relActionLog = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasActionLog", mpWorkItemLibrary); //Get the projection for the incident from the emo EnterpriseManagementObjectProjection emopIncident = new EnterpriseManagementObjectProjection(emoIncident); //Add relationship and save emopIncident.Add(objectActionLog, relActionLog.Target); emopIncident.Commit(); return(true); } catch { return(false); } }
protected override void ProcessRecord() { try { ManagementPackClass clsIncident = SMHelpers.GetManagementPackClass(ClassTypes.System_WorkItem_Incident, SMHelpers.GetManagementPack(ManagementPacks.System_WorkItem_Incident_Library, _mg), _mg); EnterpriseManagementObjectProjection incidentProjection = new EnterpriseManagementObjectProjection(_mg, clsIncident); WriteVerbose("Setting basic properties"); incidentProjection.Object[clsIncident, "Id"].Value = incidentPrefix; incidentProjection.Object[clsIncident, "Title"].Value = this.Title; if (CreatedDate != null) { incidentProjection.Object[clsIncident, "CreatedDate"].Value = this.CreatedDate; } SMHelpers.UpdateIncident(_mg, clsIncident, incidentProjection, this.Impact, this.Urgency, this.Status, this.Classification, this.Source, this.SupportGroup, null, null, this.Description, null); if (AffectedCIs != null) { WriteVerbose("Adding affected CIs"); foreach (var item in AffectedCIs) { WriteVerbose(string.Format("Adding {0} as affected configuration item.", item.Object.DisplayName)); SMHelpers.AddAffectedCI(incidentProjection, item.Object, _mg); } } if (AffectedUser != null) { WriteVerbose(string.Format("Adding {0} as affected configuration item.", AffectedUser)); SMHelpers.AddAffectedUser(incidentProjection, this.AffectedUser, _mg); } // a bulk operation // do in batches of toCommit (set above) if (Bulk) { toCommit++; idd.Add(incidentProjection); if (toCommit >= batchSize) { idd.Commit(_mg); idd = new IncrementalDiscoveryData(); toCommit = 0; } } else { incidentProjection.Commit(); } if (PassThru) { //Pass the new object to the pipeline WriteObject(incidentProjection); } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "NewIncident", ErrorCategory.InvalidOperation, Title)); } }
//Modified from Anton Gritsenko/Rob Ford on TechNet //https://social.technet.microsoft.com/Forums/WINDOWS/en-US/1f06e71c-00f4-4cf7-9f7e-a9a78b4b907c/creating-action-log-entry-for-incident-via-sdk-in-c?forum=customization //Follows similiar SMLets Exchange Connector logic //https://github.com/AdhocAdam/smletsexchangeconnector/blob/5e488ed24ff9467e0a8691df56cc993eabfb9c7c/smletsExchangeConnector.ps1#L1876 public String AddToActionLog(EnterpriseManagementGroup emg, EnterpriseManagementObject WorkItem, string Comment, string User, string CommentType) { try { //Get the System.WorkItem.Library mp ManagementPack mpWorkItemLibrary = emg.ManagementPacks.GetManagementPack(new Guid("405D5590-B45F-1C97-024F-24338290453E")); //Get the Action Log class, only 1 of 2 could be incoming - User Comments or Analyst Comments /* * ManagementPackClass CommentLogType = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.AnalystCommentLog", mpWorkItemLibrary); //UserCommentLog * ManagementPackClass UserCommentLog = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.UserCommentLog", mpWorkItemLibrary); //AnalystComments */ string commentClassName = "System.WorkItem.TroubleTicket." + CommentType; ManagementPackClass CommentLogType = emg.EntityTypes.GetClass(commentClassName, mpWorkItemLibrary); //Create a new action log entry CreatableEnterpriseManagementObject objectActionLog = new CreatableEnterpriseManagementObject(emg, CommentLogType); //Check description Comment += "\n"; if (Comment.Length > 4000) { Comment = Comment.Substring(0, 4000); } //Setup the action log entry Guid NewCommentGuid = Guid.NewGuid(); objectActionLog[CommentLogType, "Id"].Value = NewCommentGuid.ToString(); objectActionLog[CommentLogType, "DisplayName"].Value = NewCommentGuid.ToString(); objectActionLog[CommentLogType, "Comment"].Value = Comment; objectActionLog[CommentLogType, "EnteredBy"].Value = "Azure Translate/" + User; objectActionLog[CommentLogType, "EnteredDate"].Value = DateTime.Now.ToUniversalTime(); //Get the enumeration and relationship ManagementPackRelationship wiActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItemHasCommentLog", mpWorkItemLibrary); ManagementPackRelationship userActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasUserComment", mpWorkItemLibrary); ManagementPackRelationship analystActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasAnalystComment", mpWorkItemLibrary); //Get the projection for the incident EnterpriseManagementObjectProjection emopWorkItem = new EnterpriseManagementObjectProjection(WorkItem); //Add the comment and save ManagementPackRelationship WorkItemActionLogRelationship = null; switch (CommentType) { case "UserCommentLog": { WorkItemActionLogRelationship = userActionLogRel; } break; case "AnalystCommentLog": { WorkItemActionLogRelationship = analystActionLogRel; objectActionLog[CommentLogType, "IsPrivate"].Value = false; } break; } //change the relationship if the work item is a Service Request if (WorkItem.LeastDerivedNonAbstractManagementPackClassId.ToString() == "04b69835-6343-4de2-4b19-6be08c612989") { WorkItemActionLogRelationship = wiActionLogRel; } //write the new comment into the Action Log emopWorkItem.Add(objectActionLog, WorkItemActionLogRelationship.Target); emopWorkItem.Commit(); //return the new Comment Guid to notify on return(objectActionLog.Id.ToString()); } catch { return(null); } }
public String Copy() { string strWorkItemID = null; #region MPVariables ManagementPack mpIncidentLibrary = Common.GetManagementPackByName(Constants.strManagementPackIncidentLibrary, this.EMG); ManagementPackTypeProjection mptpIncident = Common.GetManagementPackTypeProjectionByName(Constants.strTypeProjectionIncident, Constants.strManagementPackIncidentManagementLibrary, this.EMG); ManagementPackClass mpcIncident = Common.GetManagementPackClassByName(Constants.strClassIncident, Constants.strManagementPackIncidentLibrary, this.EMG); ManagementPackRelationship mprCreatedByUser = Common.GetManagementPackRelationshipByName(Constants.strRelationshipCreatedByUser, Constants.strManagementPackWorkItemLibrary, this.EMG); ManagementPackEnumeration mpeIncidentStatusActive = Common.GetManagementPackEnummerationByName(Constants.strEnumerationIncidentStatusActive, Constants.strManagementPackIncidentLibrary, this.EMG); ManagementPackRelationship mprWorkItemRelatesToWorkItem = Common.GetManagementPackRelationshipByName(Constants.strRelationshipWorkItemRelatesToWorkItem, Constants.strManagementPackWorkItemLibrary, this.EMG); #endregion string strIncidentIDPrefix = GetIncidentIDPrefix(); //Create the criteria to get the object by the Work Item ID passed in String strIncidentByIDCriteria = String.Format(@"<Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">" + "<Expression>" + "<SimpleExpression>" + "<ValueExpressionLeft>" + "<Property>$Target/Property[Type='System.WorkItem.Incident']/Id$</Property>" + "</ValueExpressionLeft>" + "<Operator>Equal</Operator>" + "<ValueExpressionRight>" + "<Value>{0}</Value>" + "</ValueExpressionRight>" + "</SimpleExpression>" + "</Expression>" + "</Criteria>", this.IDToCopy); ObjectProjectionCriteria opcIncidentByID = new ObjectProjectionCriteria(strIncidentByIDCriteria, mptpIncident, mpIncidentLibrary, this.EMG); //Get the incident type projection by ID IObjectProjectionReader <EnterpriseManagementObject> emopIncidents = this.EMG.EntityObjects.GetObjectProjectionReader <EnterpriseManagementObject>(opcIncidentByID, ObjectQueryOptions.Default); foreach (EnterpriseManagementObjectProjection emopIncident in emopIncidents) { //Note: We are using foreach here but there will be only one since we are searching by the work item ID if (this.PropertiesToExclude.Length == 0) { //A list of Properties to exclude was not passed in so we are going to go with a default list. this.PropertiesToExclude = new string[] { Constants.strPropertyId, Constants.strPropertyCreatedDate, Constants.strPropertyStatus, Constants.strPropertyTargetResolutionTime, Constants.strPropertyResolutionCategory, Constants.strPropertyResolutionDescription, Constants.strPropertyClosedDate, Constants.strPropertyDisplayName }; } //Copy all the properties (including extended properties, except for those specified EnterpriseManagementObjectProjection emopNewIncident = Common.CreateNewObjectProjectionFromExistingObjectProjection(emopIncident, mpcIncident, this.PropertiesToExclude, this.EMG); //Set the ID, DisplayName, Status, and CreatedDate properties emopNewIncident.Object[mpcIncident, Constants.strPropertyStatus].Value = mpeIncidentStatusActive; emopNewIncident.Object[mpcIncident, Constants.strPropertyId].Value = String.Format("{0}{1}", strIncidentIDPrefix, "{0}"); emopNewIncident.Object[mpcIncident, Constants.strPropertyCreatedDate].Value = DateTime.Now.ToUniversalTime(); emopNewIncident.Object[mpcIncident, Constants.strPropertyDisplayName].Value = String.Format("{0} - {1}", emopNewIncident.Object[mpcIncident, Constants.strPropertyId].Value, emopNewIncident.Object[mpcIncident, Constants.strPropertyTitle].Value); if (this.RelationshipAliasesToExclude.Length == 0) { //A list of Relationships to exclude was not passed in so we are going to go with a default list. this.RelationshipAliasesToExclude = new string[] { Constants.strAliasCreatedByUser, Constants.strAliasClosedByUser, Constants.strAliasResolvedByUser, Constants.strAliasActionLogs, Constants.strAliasUserComments, Constants.strAliasAnalystComments, Constants.strAliasSMTPNotifications, Constants.strAliasActivities, Constants.strAliasFileAttachments }; } //Copy all the relationships defined in the type projection, except for those specified Common.CopyRelationships(emopIncident, ref emopNewIncident, mptpIncident, this.RelationshipAliasesToExclude); //Set CreatedByUser to be the user that is logged in EnterpriseManagementObject emoCreatedByUser = Common.GetLoggedInUserAsObject(this.EMG); if (emoCreatedByUser != null) { emopNewIncident.Add(emoCreatedByUser, mprCreatedByUser.Target); } //Relate the original incident to the new one emopNewIncident.Add(emopIncident.Object, mprWorkItemRelatesToWorkItem.Target); //And finally submit... emopNewIncident.Commit(); strWorkItemID = emopNewIncident.Object[mpcIncident, Constants.strPropertyId].Value.ToString(); } return(strWorkItemID); }