Sunday, March 23, 2014

Sharepoint - List all Site Level and Web Level features programmatically


The below sample is just for listing all site feature and current web level features.


static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://site collection url"))
            {
                Console.WriteLine("");
                Console.WriteLine("Site URL=" + site.Url);
                Console.WriteLine("");
                foreach (SPFeature f in site.Features)
                {
                    Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                }


                using (SPWeb web = site.OpenWeb())
                {
                    Console.WriteLine("");
                    Console.WriteLine("\n\n Web URL=" + web.Url + "\n\n");
                    Console.WriteLine("");
                    foreach (SPFeature f in web.Features)
                    {
                        Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                    }
                }
            }
            Console.WriteLine("");
            Console.WriteLine("Program Completed");

        }



If you want to display all web level features from all the subwebs, you need to write a recursive function to loop through all subsites and display the features.

No comments:

Post a Comment