1

I'm trying to pull the Requirements folder structure from QC using C#. The OTA has a built in treeview for testplans but not for requirements. So far I'm able to pull all the folder under root but all the folders are just children of root. See below

-Requirements
   -folder 1
   -folder 1's child
   -folder 2
   -folder 2's child

What I want is

-Requirements
   -folder 1
      -folder 1's child
   -folder 2
      -folder 2's child

Here is my code

protected void Page_Load(object sender, EventArgs e)
    {
        TreeView1.Nodes.Clear();
        TDConnection td = new TDConnection();
        string serverURL = connectstr;
        td.InitConnectionEx(serverURL);
        td.ConnectProjectEx("test", "test", "test", "test");
        bool checkConnect = td.LoggedIn;
        ReqFactory reqFactory = (ReqFactory)td.ReqFactory;
        HierarchyFilter reqFilter = (HierarchyFilter)reqFactory.Filter;
        reqFilter.KeepHierarchical = true;
        reqFilter["RQ_FATHER_ID"] = "0";
        HierarchySupportList reqs = (HierarchySupportList)reqFilter.NewList();
        TreeNode tn = new TreeNode("Requirements");
        TreeView1.Nodes.Add(tn);
        IterateOverReqs(reqFilter, reqs, tn);

    }

    private void IterateOverReqs(HierarchyFilter reqFilter, HierarchySupportList reqs, TreeNode tn)
    {
        TreeNode t = new TreeNode();
        foreach (Req req in reqs)
        {
            if (req.IsFolder)
            {
                reqFilter["RQ_FATHER_ID"] = "" + req.ID;
                HierarchySupportList childReqs = (HierarchySupportList)reqFilter.NewList();
                t = new TreeNode((string)req.Name);
                tn.ChildNodes.Add(t);
                IterateOverReqs(reqFilter, childReqs, tn);  
            }
        }
1

I think the issue here might be in your recursive call to IterateOverReqs. You pass in the parameter tn which is the top node, therefore, all your requirements are being added to that top node.

Instead, you should pass in the t node so that lower level requirements are added to the node just above it instead of the top node.

private void IterateOverReqs(HierarchyFilter reqFilter, HierarchySupportList reqs, TreeNode tn)
{
    TreeNode t = new TreeNode();
    foreach (Req req in reqs)
    {
        if (req.IsFolder)
        {
            reqFilter["RQ_FATHER_ID"] = "" + req.ID;
            HierarchySupportList childReqs = (HierarchySupportList)reqFilter.NewList();
            t = new TreeNode((string)req.Name);
            tn.ChildNodes.Add(t);

            // Change the below line so that it passes t instead of tn
            IterateOverReqs(reqFilter, childReqs, t);  
        }
    }
}
0

It looks like your tree creation is correct. So I am assuming it is the tree traversal that you are using that is giving you the incorrect output. You would need to do a pre-order traversal and output that correctly given the tree level you are at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.