site collection FeatureDefinitions empty RRS feed

  • Question

  • Hi,

    I created a site collections with a custom site definition and added features by a stapler. now I want to read all available feature definitions in my site collection. I access the property site.FeatureDefinitions, but the returned SPFeatureDefinitioncollection is empty.

    When I manage the features through the interface all standard SharePoint features and all my custom features are available.

    I would guess that if I see everything in the interface, the SPFeatureDefinitionCollection should be filled a well?

    Does anybody has an idea why it's empty?

    Friday, October 29, 2010 9:52 AM

All replies

  • Hi sprober,

    can you please post the complete code how you request the FeatureDefintions.

    Regards Stefan

    MCTS - SharePoint / WSS Configuration and Development
    Sunday, October 31, 2010 5:05 PM
  • using(SPSite site = new SPSite("http://localhost")
     foreach(SPFeature feature in site.Features)

    This code would write out the Activated Featues. 




    Dennis Bottjer | Follow Me: @dbottjer | Blog: Dennis Bottjer.com
    Monday, November 1, 2010 3:10 AM
  • I don't want all activated features. I want all features that are avaible in my spsite.


    class Program

    static void Main(string[] args)

      Console.WriteLine("Program Started");

      using (SPSite site = new SPSite(http://aSiteCollection.be/sites/test_312))
    SPFeatureDefinitionCollection defColl = site.FeatureDefinitions;

         foreach (SPFeatureDefinition def in defColl)

      Console.WriteLine("Program Finished");

    This code gives no results. When I debug the property FeatureDefinitions returns an empty collection.

    Any suggestions are welcome:).





    Wednesday, November 3, 2010 9:30 AM
  • Hello sprober,

    as I understand solutions and features in SP2010 it like this. You upload a solution to the server and you deploy it. From that point it's available for the whole farm and since features are primarily limited by it's scope and not by a sitecollection. In site settings -> sitecollection features you can see all features deployed with scope site.

    So my approach was to look through all available featuers and filter those who got the scope I'm looking for.

    foreach (SPFeatureDefinition feature in SPFarm.Local.FeatureDefinitions)
     if (feature.Scope == SPFeatureScope.Site)
      // Find the feature you're looking for<br/>
      yourFeature = feature;<br/>

    After finding the feature you're looking for you could activate it for your site with one line of code

    site.Features.Add(yourFeature.Id, true);
    Hope this helps.

    Wednesday, November 3, 2010 10:43 AM
  • Hi,

    That is indeed the approach I use to activate features. But in this case I want to disable certain features at certain site collections (the same as what happens with the cmdLet Disable-SPFeature).

    To do this it seems logical to me that I should manipulate the SPFeatureDefinitionsCollection at site collection level. The only problem is that it appears to be empty to begin with... :-)

    Wednesday, November 3, 2010 11:04 AM
  • If I understand you correctly you're looking for the code dbottjer already posted. That way you only get the activated features for your sitecollection or if you're looking for the activated features with scope "web" your should look into SPWeb.Features, but from what you've posted dbottjer pointed out the way to go.
    Wednesday, November 3, 2010 12:10 PM
  • Hi,

    I'll try to explain myself more clearly :).

    • I install a solution to SharePoint 2010
    • It installs features in the farm by making the featuredefinitions available to all sitecollections
    • Now I want to make sure that some featuredefinitions are not available to certain sitecollections

    I think I can accomplish that goal by removing the featuredefinition from the SPSite.FeatureDefinitions collection. but when I try to access this collection it allways seems empty.

    My problem exists before activating or deactivating cetrain features. And because I don't want to touch all sitecollections the SPFarm.Local.FeatureDefinitions cannot solve my problem.

    Wednesday, November 3, 2010 12:41 PM
  • Hi, sprober


    Would you please also check permission under current context?

    To visit site collection feature, you should at least have SPBasePermissions.ManageWeb permission.

    By you code, you execute the logic in a windows console, so please confirm if current account have proper permission.

        If so, your scenario seems to turn to "SPSecurity.RunWithElevatedPrivileges or Impersonation in Console Applications".

        Would you please see the discussion and test in the following article:



        In this situation, we use RunAs[http://technet.microsoft.com/en-us/library/cc781769(WS.10).aspx] command for impersonation.

        Hope this can help.


    Best Regards,


    • Proposed as answer by Stefan BauerMVP Friday, November 5, 2010 7:07 AM
    • Unproposed as answer by sprober Friday, November 5, 2010 8:37 AM
    Friday, November 5, 2010 6:51 AM
  • I checked my Users permissions and it is a site collection administrator, but I get no results.

    Then I tried the runas-approach with my application pool account, but still no results.

    My code:

      public Program()
       using (SPSite site = new SPSite("http://MyUrl.be/sites/test_312"))
        Console.WriteLine("Is site admin: " + site.RootWeb.UserIsSiteAdmin.ToString());
        SPFeatureDefinitionCollection defColl = site.FeatureDefinitions;
        foreach (SPFeatureDefinition def in defColl)


    Is site admin: True

    The foreach-loop still gives no results.

    Friday, November 5, 2010 8:35 AM
  • Hi sober,

    what is the target platform for you console application. Do you have set it to 64bit because outherwise you console application will fail too.

    You should check out:


    kind regards

    twitter: http://twitter.com/n8des1gn
    MCTS - SharePoint / WSS Configuration and Development
    Saturday, November 13, 2010 8:47 AM
  • Hi Stefan,

    My target platform is 64bit. The application runs, it is just the propertie FeatureDefinitions from my SPSite-object that return an empty collection.

    Does the program returns a filled up SPFeatureDefinitioncollection in your environment? So I know it's a problem with my environment and not a bug in SharePoint.

    Kind regards

    Monday, November 15, 2010 8:14 AM
  • Hi. Did you resolve this? I have the same problem...
    Wednesday, February 22, 2012 3:04 PM
  • I've the same problem too. Looking for a solution.

    NLV - MCTS - Blog - Twitter - In

    Wednesday, October 30, 2013 12:12 PM