每次功能激活/停用时都会重复添加功能元素

发布于 2024-08-28 21:42:18 字数 1072 浏览 1 评论 0原文

与整个范围相比,这是一个非常小的行为,但我想阻止它。

我创建了一个非常非常简单的 SharePoint 功能。它的清单中有两个元素:一个 aspx webpart 页面和一个 elements xml。我将在下面解释我的 elements xml,它只是添加一个模块。

 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="Pass" Url="" Path="">
      <File Url="pasq.aspx" NavBarHome="True" Type="Ghostable">
        <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="0">
                  <![CDATA[
                   WARGH THIS PART DOESN'T MATTER FOR THIS QUESTION!
                  ]]>
        </AllUsersWebPart>
      </File>
    </Module>
</Elements>

现在,在我第一次部署并激活此功能时,它可以正常工作。但是,如果我必须停用然后重新激活该功能才能修复 Web 部件中的某些属性,那么我会发现页面上有第二个 Web 部件。当然,每个类似的周期都会增加越来越多的东西。我知道必须创建一个新的 Web 部件才能应用我刚刚所做的更改,但为什么旧的 Web 部件仍然在页面上?我可以让这个旧的 Web 部件作为功能激活/停用过程的一部分自动消失,而无需使用 Receiver 类吗?

编辑 基于我已经使用接收器来 解决这个问题,我最终只是添加了 Web 部件删除作为功能停用的一部分。但我仍然希望也许我错过了一些东西。

This is a very minor behavior when compared with the entire scope, but it is one that I'd like to put a stop to.

I have created a very, very simple SharePoint Feature. It has two elements in its manifest: an aspx webpart page, and an elements xml. I'll paraphrase my elements xml, which just adds a module, below.

 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="Pass" Url="" Path="">
      <File Url="pasq.aspx" NavBarHome="True" Type="Ghostable">
        <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="0">
                  <![CDATA[
                   WARGH THIS PART DOESN'T MATTER FOR THIS QUESTION!
                  ]]>
        </AllUsersWebPart>
      </File>
    </Module>
</Elements>

Now, on the first time I deploy and activate this feature, it works properly. But if I have to deactivate and then reactivate the feature in order to fix some properties in the webpart, then I find myself with a second webpart on the page. Naturally, each similar cycle will just add more and more. I understand that a new webpart must be created in order to employ the changes I just made, but why is the old webpart still on the page? Can I make this older webpart go away automatically as part of the feature activation/deactivation process without needing to employ a Receiver class?

EDIT
Based on the fact that I've already employed a Receiver to solve this issue, I ended up just adding the webpart removal as part of feature deactivation. But I was still hoping that maybe there's something I'm missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送你一个梦 2024-09-04 21:42:18

不幸的是,除非您执行以下操作之一,否则内容将重复:

解决方案 1:您可以向激活功能添加类似的代码,以在页面中添加 Web 部件,如下所示:

SPFile file = page.File;
using (SPLimitedWebPartManager WebPartManager =
        file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
    try
    {
        SPLimitedWebPartCollection webparts = WebPartManager .WebParts;
        if (webparts.Count == 0)
        {
            //set your web part 
            WebPartManager.AddWebPart(webpart, "MomoZone", 1);
        }
    }
    catch(Exception ex)
    {
        throw;
    }
    finally
    {
        WebPartManager.Web.Dispose();
    }
}

解决方案 2:您可以将站点定义与运行的 Onet 文件一起使用只有在网站创建后,您在将 efatures 设置为隐藏

Cheers时就不会再遇到此问题

Unfortunately the content will be repeated unless you do one of the following :

Solution 1 : you can add a similar code to the activate feature to add webparts in the pages as follows:

SPFile file = page.File;
using (SPLimitedWebPartManager WebPartManager =
        file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
    try
    {
        SPLimitedWebPartCollection webparts = WebPartManager .WebParts;
        if (webparts.Count == 0)
        {
            //set your web part 
            WebPartManager.AddWebPart(webpart, "MomoZone", 1);
        }
    }
    catch(Exception ex)
    {
        throw;
    }
    finally
    {
        WebPartManager.Web.Dispose();
    }
}

Solution 2 : you can use site definition with and Onet file that runs only once the site is created so you would have no more of this problem while setting your efatures to hidden

Cheers

柠栀 2024-09-04 21:42:18

SharePoint 不提供声明性方式 (xml) 来自动删除功能元素。抱歉,您必须在功能接收器上的 FeatureDeactiving 方法中添加一些代码才能以编程方式将其删除。

SharePoint doesn't offer a declarative way (xml) to remove feature elements automagically. You'll have to add some code in a FeatureDeactiving method on the feature receiver to remove it programatically, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文