简体   繁体   中英

Deactivating web part feature in sharepoint

I've developed a web part using the wspbuilder tool (Web part with feature).

When deployed (to a site collection), you have to activate the feature in order to use this web part - so far so good.

However, when deactivating the feature, the web part remains on any site where it's been added, and furthermore, it's still available in the web part gallery?

Is this expected behaviour? Is there no way of removing the web part from all subsites in the site collection, and also remove it from the web part gallery?

Thanks ;)

This is expected behavior. You could remove it automatically, but you would have to write a feature receiver to accomplish that.

More info about creating a feature receiver for SharePoint 2007: http://sharepointdevwiki.com/display/public/How+to+add+a+Feature+Receiver+to+a+Feature

You can do it by using the following code:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;

    if (site != null)
    {
        try
        {
            SPList wpGallery = site.GetCatalog(SPListTemplateType.WebPartCatalog);
            SPQuery query = new SPQuery();
            query.Query = “<Where><Eq><FieldRef Name=’FileLeafRef’ /><Value Type=’Text’>webpartname.webpart</Value></Eq></Where>”;
            SPListItemCollection items = wpGallery.GetItems(query);

            if (items.Count > 0)
            {
                items[0].Delete();
            }
        }
        catch { }
    }
}

WSPBuilder automatically adds the removal event receiver to the feature when you create a web part. Very convenient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM