QC: How to get the Requirements Folder structure using OTA?



  • 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);  
                }
            }
    


  • 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);  
            }
        }
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2