简体   繁体   English

在SharePoint中停用Web部件功能

[英]Deactivating web part feature in sharepoint

I've developed a web part using the wspbuilder tool (Web part with feature). 我已经使用wspbuilder工具开发了一个Web部件(具有功能的Web部件)。

When deployed (to a site collection), you have to activate the feature in order to use this web part - so far so good. 部署(到网站集)后,您必须激活功能才能使用此Web部件-到目前为止非常好。

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? 但是,当停用该功能时,Web部件仍保留在添加了该部件的任何站点上,此外,Web部件库中是否仍提供该部件?

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? 是否无法从网站集中的所有子站点中删除Web部件,也无法将其从Web部件库中删除?

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 有关为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. 创建Web部件时,WSPBuilder会自动将删除事件接收器添加到功能中。 Very convenient. 很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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