There is a farm feature and there is a web-scoped feature. Web feature does stuff on multiple sites. If ever i want to get rid of the feature on all sites, I have a farm feature that has on deactivate following code, while on-activate is empty(no logic): Is there something wrong with my code?? I get and error:

value does not fall within the expected range

          public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        SPSite tmpRoot = new SPSite("http://omac-spsdev01");
        SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;
        foreach (SPSite site in tmpRootColl)
            foreach (SPWeb web in site.AllWebs)
                string FeatureID = "5f71dfd4-20dd-432f-ae60-60e31f5e972b";
                //Get feature Guid  
                Guid myfeatureid = new Guid(FeatureID);

                //remove feature

                //SPFeatureCollection features = web.Features;
                //SPFeature myfeature = features[FeatureID];

                if (web.Features[myfeatureid] != null)   //Make sure Feature is installed!
                    web.Features.Remove(myfeatureid);//deactivate feature


| improve this question | | | | |
  • Can you provide a StackTrace, so we can see what line is actually causing the error? – Neil White Sep 13 '12 at 19:18
  • I have updated the trace log.. I am so mad right now I wanna get to the bottom of this $hit – Sarika thapaliya Sep 13 '12 at 21:38

SPWeb.Features only contains activated features. If you call web.Features[myfeatureid]; on a web that does not have that feature activated then it will blow up in the exact manner you describe.

Updated with code - you want something like this:

if (oWeb.Features[id] != null)

(The looping I had talked about before was actually for event receivers and not features so I have since removed that remark - I apologize for the confusion)

| improve this answer | | | | |
  • I have updated my code, but it still throws the same error!!! please help! – Sarika thapaliya Sep 13 '12 at 19:05
  • The updated code don't check if your feature is activated, it only checks that at least one feature is activated. In fact now it'll break on all sites as it'll try to deactivate you feature as many times as there are features activated – Per Jakobsen Sep 13 '12 at 19:54
  • Will this do it? if ((myfeature != null) & (myfeature.DefinitionId==myfeatureid)) //Make sure Feature is installed! { web.Features.Remove(myfeatureid);//deactivate feature } – Sarika thapaliya Sep 13 '12 at 20:03
  • Updated with code. Let me know how it works. – SpartanDonut Sep 13 '12 at 20:08
  • @ToddersLegrande Now I have a different error: "Object reference not set to an instance of an object.".. – Sarika thapaliya Sep 13 '12 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.