Guys, this can useful where you wanna get the properties associated to the Term Store Management Tools:
You create a Windows Forms where you will have to add a Button, TextBox for Site Url Input and a a DataGridView for Output and Paste the below Code.
You create a Windows Forms where you will have to add a Button, TextBox for Site Url Input and a a DataGridView for Output and Paste the below Code.
private void button1_Click(object sender, EventArgs e){try{string url = textBox1.Text;SPSecurity.RunWithElevatedPrivileges(delegate{using (SPSite site = new SPSite(url)){TaxonomySession TaxonomySession = new TaxonomySession(site);TermStoreCollection TermStoreCollection = TaxonomySession.TermStores;{DataTable table = CreateTable();foreach (TermStore ts in TermStoreCollection){DataRow rowTS = table.NewRow();rowTS["ServiceAppName"] = ts.Name;rowTS["ServiceAppID"] = ts.Id;rowTS["ServiceAppIsOnline"] = ts.IsOnline;table.Rows.Add(rowTS);if (ts.IsOnline){#region MainTermStore TermStore = TaxonomySession.TermStores[ts.Name];GroupCollection groups = TermStore.Groups;foreach (Group grp in groups){DataRow rowGroup = table.NewRow();rowGroup["GroupName"] = grp.Name;rowGroup["GroupId"] = grp.Id;table.Rows.Add(rowGroup);string termStoreGroup = grp.Name;var cTermStore = TermStore.Groups.Where(s => s.Name == termStoreGroup);if (cTermStore.ToList().Count > 0){//Group group = TermStore.Groups[termStoreGroup];foreach (var termSets in grp.TermSets){DataRow rowTermSets = table.NewRow();rowTermSets["TermSetsID"] = termSets.Id;rowTermSets["TermSetsName"] = termSets.Name;table.Rows.Add(rowTermSets);foreach (var terms in termSets.Terms){DataRow rowTerms = table.NewRow();rowTerms["TermID"] = terms.Id;rowTerms["TermName"] = terms.Name;table.Rows.Add(rowTerms);foreach (var subTerms in terms.Terms){DataRow rowSubTerms = table.NewRow();rowSubTerms["SubTermID"] = subTerms.Id;rowSubTerms["SubTermName"] = subTerms.Name;table.Rows.Add(rowSubTerms);}}}}else{MessageBox.Show("Group not found");}}#endregion}}dataGridView1.DataSource = table;}}});}catch (Exception ex){MessageBox.Show(ex.Message);}}private DataTable CreateTable(){try{DataTable table = new DataTable();table.Columns.Add("ServiceAppName");table.Columns.Add("ServiceAppID");table.Columns.Add("ServiceAppIsOnline");table.Columns.Add("GroupName");table.Columns.Add("GroupID");table.Columns.Add("TermSetsName");table.Columns.Add("TermSetsID");table.Columns.Add("TermName");table.Columns.Add("TermID");table.Columns.Add("SubTermName");table.Columns.Add("SubTermID");return table;}catch (Exception ex){MessageBox.Show(ex.Message);return null;}}
No comments:
Post a Comment