Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 "value does not fall within the expected range" error...

          public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        System.Diagnostics.Debugger.Break();

        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
                }
                web.Dispose();
            }

            site.Dispose();
        }

    }
share|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
add comment

1 Answer 1

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)
{
     oWeb.Features.Remove(id);
}

(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)

share|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. –  ToddersLegrande 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
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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