AnswerBun.com

How to deactivate feature from site collecion programmatically?

SharePoint Asked by Ola on February 15, 2021

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;
                }
            }
        }

One Answer

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;
}    

Answered by ECM4D on February 15, 2021

Add your own answers!

Related Questions

Like the List created and not the list item

2  Asked on January 27, 2021 by antra-sharma

     

SP 2016 – Only TTFB slow

2  Asked on January 16, 2021 by mhouston100

 

Unable to upload to SharePoint Online via VBA

1  Asked on January 11, 2021 by leroy

   

How to Create WF with C#

2  Asked on January 8, 2021 by whatsup

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP