views:

109

answers:

2

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/"&gt;
    <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.

A: 

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.

x0n
Considering that other parts of the elements manifest (content types, field types, workflows, custom actions) cease to be when a feature is deactivated, I had hoped it would be consistent. Your answer suffices, thanks for your help @x0n
ccomet
A: 

Hi

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

Mohamed Hachem