Wednesday, December 21, 2016

Example of csom looping recursively through termstore

  private void addNodes(TreeNode treeNode,Term term,string source = "Root")
        {
            if (term.Terms.Count > 0)
            {
                foreach (Term t in term.Terms)
                {
                    TreeNode tn = new TreeNode(string.Format("{0}  - {1}", t.Name, t.Id.ToString()));
                    treeNode.Nodes.Add(tn);
                    status.Text = string.Format("{0}-{1}", source, t.Name);
                    Application.DoEvents();
                    context.Load(t.Terms);
                    context.ExecuteQuery();
                    if (t.Terms.Count > 0)
                    {
                        addNodes(tn, t, string.Format("{0}-{1}", source, t.Name));
                    }
                }
            }
        }

 //calling code here
   foreach (Term t in ts.Terms)
            {
                TreeNode tn = new TreeNode(string.Format("{0}  - {1}", t.Name,t.Id.ToString()));
                status.Text = t.Name;
                Application.DoEvents();
                    //tn.Nodes.Add(string.Format("{0}",t.Id.ToString()));
                if (Recursive.Checked && (item.Text.Length == 0 || t.Name.Equals(item.Text, StringComparison.OrdinalIgnoreCase )))
                {
                    context.Load(t.Terms);
                    context.ExecuteQuery();
                    if (t.Terms.Count > 0)
                    {

                        addNodes(tn, t, t.Name);
                    }
                }
               treeNode.Nodes.Add (tn);
            }
          

            TV.Nodes.Add(treeNode);

No comments:

Post a Comment