Saturday, July 18, 2009

Enumerate SharePoint Features

. Saturday, July 18, 2009

Enumerate Installed Features in the Farm

TreeView TreeView1 = new TreeView();

TreeView1.Nodes.Add(new TreeNode("Features"));
TreeNode oFSiteNode = null;
TreeNode oFNode = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{

//get the name of the current web in current user's context

                string spSiteURL = SPControl.GetContextSite(Context).Url.ToString();

                //Initialize the spweb object using the elevated privileges
                using (SPSite oSite = new SPSite(spSiteURL))
                {


TreeView1.Nodes.Clear();

#region Return Installed Features.

                    oFSiteNode = new TreeNode("All Farm Installed Features");

                    SPFarm oFarm = oSite.WebApplication.Farm;

                    SPFeatureDefinitionCollection oFeatDefColl = oFarm.FeatureDefinitions;
                    foreach (SPFeatureDefinition oFeatDef in oFeatDefColl)
                    {
                        if (oFeatDef != null)
                        {
                            oFNode = new TreeNode(oFeatDef.GetTitle(new System.Globalization.CultureInfo(1033)));
                            oFSiteNode.ChildNodes.Add(oFNode);
                        }
                    }

                    TreeView1.Nodes[1].ChildNodes.Add(oFSiteNode);

#endregion

TreeView1.CollapseAll();
oSite.Dispose();

}

}

0 comments: