1

How to deactivate feature from site collection? I am trying the code below but got this error:

Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb".

private void DeactivateProvisioningFeature()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    site.AllowUnsafeUpdates = true;

                    var feature = site.Features.SingleOrDefault(sf => sf.DefinitionId == new Guid("464b78de-c14e-4c88-bd52-00136fc899f4")); // find delegate control provisioning feature
                    if (feature != null) //if feature is activated
                    {
                        site.Features.Remove(feature.DefinitionId, true); //deactivate feature
                    }

                    web.Update();

                    site.AllowUnsafeUpdates = false;
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
  • What if you bring back SPSecurity.RunWithElevatedPrivileges? – Paul Strupeikis Apr 24 '15 at 11:28
  • Is it realy needed to use the runwithelevatedpriveleges to deactivate a site collection feature? – Ola Apr 24 '15 at 11:29
  • 1
    did you try web.AllowUnsafeUpdates = true; Web.Update(); – Waqas Sarwar MVP Apr 24 '15 at 11:51
  • Waqas, please see my code above. They were already available. – Ola Apr 24 '15 at 11:55
  • If you don't call Update, you are just setting a boolean that never gets applied to the site or web. – Eric Alexander Apr 24 '15 at 12:08
0

You do NOT need and you should NOT call web.update(). This mean that you save this property to the database.

To disable site collection feature, you do not need SPWeb instance also.

If you have permissions to manage features of the site, you also do not need new SPSite object instance.

Try to write you code like, this:

if (SPContext.Current.Site != null)
{
    site = SPContext.Current.Site;
    site.AllowUnsafeUpdates = true;

    //YOUR FEATURE DEACTIVATION CODE

    site.AllowUnsafeUpdates = false;
}    

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.