开发者

Feature element repeatedly added with every feature activation/deactivation

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开发者_C百科, 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜