Tuesday, May 20, 2014

Term Store Management and Get Parent Child Name - Guid - Properties

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.

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 Main

                                        TermStore 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;
            }
        }

Wednesday, May 14, 2014

ToolbarType "ShowToolbar" of SharePoint ListViewWebPart

Helper method to Set the "ShowToolbar" of ListViewWebPart Toolbar


 private void SetToolbarType(SPWeb web)
        {
            try
            {
                string url = web.Url + "/Pages/defaultlenze.aspx";
                SPLimitedWebPartManager splwManager = web.GetLimitedWebPartManager(url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                SPFile file = splwManager.Web.GetFile(url);
                file.CheckOut();
                foreach (WebPart webPart in splwManager.WebParts)
                {
                    if (webPart is Microsoft.SharePoint.WebPartPages.ListViewWebPart)
                    {
                        Guid webPartGuid = new Guid(((Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPart).ViewGuid);
                        string listName = string.Empty;
                        if (webPart.Title == "My Active Task")
                        {
                            listName = "Tasks";
                        }
                        else if(webPart.Title == "News")
                        {
                            listName = "News";  
                        }
                        SPView view =  web.Lists[listName].Views[webPartGuid];
                        Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
                        MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
                        object[] setToolbarParam = { "ShowToolbar" }; //set the type here
                        setToolbarTypeMethod.Invoke(view, setToolbarParam);
                        view.Update();
                    }
                }
                file.CheckIn("");
                file.Publish("");
            }