Post

2 followers Follow
0
Avatar

Examples to use RecursiveQuery or WorkItemRecursiveQuery?

Hello,

I'm trying to use RecursiveQuery or WorkItemRecursiveQuery but don't really get how to use them. Does anyone has some working examples?

On my first test, I try to recursively retrieve the parents of the root WorkItem.

So far, I'm not expecting an "out of the box" solution (but that'd be great too). First, I'd like to get how to use it in order to write my own Recurcive Queries.

Thank you for reading, and for you feedbacks,

Armand

Armand BOLMONT Answered

Official comment

Avatar

Hi Armand,

Here is an example of working with the "RecursiveQuery" in C#:

//Login

var clarizenClient = new Clarizen { Timeout = 60 * 1000, EnableDecompression = true };
var options = new LoginOptions();
options.ApplicationId = "Sample c# client";
var srvDef = clarizenClient.GetServerDefinition("username", "password", options);
clarizenClient.Url = srvDef.ServerLocation;
var lr = clarizenClient.Login("username", "password", options);

//Use query
var recursiveQuery = new RecursiveQuery
{
   TypeName = "WorkItem",
   Fields = new[] { "Name", "Parent" },
   Levels = 1, //level of item that you want to retrieve
   RootItemId = new EntityId { TypeName = "WorkItem", Value = "1dz79zji4khiafgzfpl179p5d5792" } //Id of the root work item
};
var recursiveQueryResults = clarizenClient.Execute(new[] { recursiveQuery });
if (recursiveQueryResults.Any())
{
   var recursiveQueryResult = (QueryResult)recursiveQueryResults.First();
   if (recursiveQueryResult.Success)
   {
      foreach (var entity in recursiveQueryResult.Entities)
      {
           Console.WriteLine(new String('*', 15));
           Console.WriteLine("Work Item Id - " + entity.Id.Value);
           Console.WriteLine("Work Item Name - " + entity.Values[0].Value);
           if (entity.Values[1].Value != null)
              Console.WriteLine("Work Item Parent - " + ((Entity)entity.Values[1].Value).Id.Value);
     }
  }
}

}

 

Result:

***************
Work Item Id - 2gptooj3emdqmk3zmxeo51szq6671   //This is the root project
Work Item Name - Design Review
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792
***************
Work Item Id - 2gptooj3emdqmk3zmxeo51szq6677
Work Item Name - Mockup Ready
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792
***************
Work Item Id - 2gptooj3emdqmk3zmxeo51szq6683
Work Item Name - Engineering Sample
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792
***************
Work Item Id - 2gptooj3emdqmk3zmxeo51szq6689
Work Item Name - Customer Sample
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792
***************
Work Item Id - 1dz79zji4khiafgzfpl179p5d5808
Work Item Name - Industry Design Roadmap
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792
***************
Work Item Id - 2gptooj3emdqmk3zmxeo51szq6587
Work Item Name - Concept DCP
Work Item Parent - 1dz79zji4khiafgzfpl179p5d5792

 

I hope this helps,

Elad

Elad Franklin
Comment actions Permalink

Please sign in to leave a comment.

3 comments

0
Avatar

Thank you Elad, I'm pretty sure it will help me a lot.

I have a question about this code

Levels = 1, //level of item that you want to retrieve

Is this the level of the root work item, or is this an exit criteria for the recursive query? I'd say from the example that this is the exit criteria.

Am I correct to say that the path of your test item is:

Concept DCP > Industry Design Roadmap > Customer Sample > Engineering Sample > Mockup Ready > Design Review

with Design Review the root element, and Concept DCP the top level Work Item (with no parent)?

 

Thanks again

Armand BOLMONT 0 votes
Comment actions Permalink
0
Avatar

Hi Armand,

The level property is, as you said, the exit criteria. this means that the query will retrieve level 0 and 1.

Here is the structure of the my project:

Elad Franklin 0 votes
Comment actions Permalink