Get Child Incidents via SDK

Hello,

I'm trying to retrieve all child incidents of a specific incident (to show more details than the default "ChildIncidents" tab in the default Incident form), but unable to enumerate them.

I've tried using both the System.WorkItem.Projection and the System.WorkItem.ChildIncident.ProjectionType projection type with no luck. The code contains both methods tried.

  static void Main(string[] args)
        {
            string strIRID = "IR82210";
            // connect to management group
            EnterpriseManagementGroup mg = new EnterpriseManagementGroup("SM12-NLB");

                                            string strCriteria = @"<Criteria xmlns='http://Microsoft.EnterpriseManagement.Core.Criteria/'>
                                <Reference Id='System.WorkItem.Library' PublicKeyToken='{0}' Version='{1}' Alias='WorkItem' />
                                    <Expression>
                                    <SimpleExpression>
                                        <ValueExpressionLeft>
                                        <Property>$Context/Property[Type='System.WorkItem.Incident']/Id$</Property>
                                        </ValueExpressionLeft>
                                        <Operator>Equal</Operator>
                                        <ValueExpressionRight>
                                        <Value>{2}</Value>
                                        </ValueExpressionRight>
                                    </SimpleExpression>
                                    </Expression>
                                </Criteria>";

            ManagementPackCriteria mpCr = new ManagementPackCriteria("Name = 'System.WorkItem.Incident.Library'");
            IList<ManagementPack> mps = mg.ManagementPacks.GetManagementPacks(mpCr);
            ManagementPack incidentMp = mps != null && mps.Count > 0 ? mps[0] : null;

            ManagementPackClassCriteria classCr = new ManagementPackClassCriteria("Name = 'System.WorkItem.Incident'");
            IList<ManagementPackClass> classes = mg.EntityTypes.GetClasses(classCr);
            ManagementPackClass incidentCl = classes != null && classes.Count > 0 ? classes[0] : null;

            //ManagementPackTypeProjectionCriteria tpCr = new ManagementPackTypeProjectionCriteria("Name = System.WorkItem.ChildIncident.ProjectionType'");
            ManagementPackTypeProjectionCriteria tpCr = new ManagementPackTypeProjectionCriteria("Name = 'System.WorkItem.Projection'");

            IList<ManagementPackTypeProjection> projList = mg.EntityTypes.GetTypeProjections(tpCr);
            ManagementPackTypeProjection incidentProj = projList != null && projList.Count > 0 ? projList[0] : null;



            if (incidentMp != null && incidentCl != null && incidentProj != null)
            {
                string criteria = string.Format(strCriteria, incidentMp.KeyToken, incidentMp.Version, strIRID);
                ObjectProjectionCriteria cr = new ObjectProjectionCriteria(criteria, incidentProj, incidentMp, mg);

                IObjectProjectionReader<EnterpriseManagementObject> reader = mg.EntityObjects.GetObjectProjectionReader<EnterpriseManagementObject>(cr, ObjectQueryOptions.Default);
                foreach (EnterpriseManagementObjectProjection proj in reader)
                {
                    Console.WriteLine(proj.Object[null, "Title"]);
                    Console.WriteLine(proj["RequestedWorkItem"][0].Object[null, "Domain"]);
                    Console.WriteLine(proj["RequestedWorkItem"][0].Object[null, "UserName"]);
                    Console.WriteLine(proj["ChildWorkItem"][0].Object[null, "Id"]);

                }
            }

There are a few ways documented out there on how to retrieve relationship objects, but tried them all with no luck

Hope anyone can help me.

Thanks

                                
July 16th, 2015 11:47am

Give this a try and let me know if it works (we don't use parent - child incidents here so I don't have anything to test with)

IList<EnterpriseManagementRelationshipObject<EnterpriseManagementObject>> listOfChildren;
ManagementPackRelationship mpRelType_WorkItemHasParentWorkItem = managementGroup.EntityTypes.GetRelationshipClass(new Guid("DA3123D1-2B52-A281-6F42-33D0C1F06AB4"));
listOfChildren = managementGroup.EntityObjects.GetRelationshipObjectsWhereSource<EnterpriseManagementObject>(parentWorkItem.Id, mpRelType_WorkItemHasParentWorkItem, DerivedClassTraversalDepth.None, TraversalDepth.Recursive, ObjectQueryOptions.Default);

  • Marked as answer by Iske12 19 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 5:43pm

Awesome, that did the trick :-)

Just had to call the GetRelationshipObjectsWhereTarget instead of GetRelationshipObjectsWhereSource to get the list of child incidents.

I guess that if I used the WorkItemHasChildWorkItem relationship class I could have used GetRelationshipObjectsWhereSource

Anyway, thanks for your help

July 22nd, 2015 3:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics