Compartilhar via


How to: Add Elements to an Existing Feature

Applies to: SharePoint Foundation 2010

A basic Feature upgrade scenario is likely to include the steps listed in the following procedure, where new elements are added to an existing Feature.

Adding new elements to an existing Feature

  1. Add a Version attribute to Feature.xml, if it does not already have one, and set it to a likely value, for example, Version="2.0.0.0" if the existing version is 1.0.0.0. If you do not increase the version number, Feature upgrade code will not be called.

  2. Add an <UpgradeActions> section to the Feature.xml file.

  3. Create a new Elements.xml file that contains all the new elements that you are adding to the Feature, and give it a unique name, for example, Elements2.xml.

  4. Reference the Elements2.xml file in both the main <ElementManifests> section and the <ApplyElementManifests> section within the <UpgradeActions> section, as shown in the following example.

    <Feature Id="712224F9-6708-4965-A18C-B73CA86AEFCA"
      Title="MyCoolFeature" 
      Description="My cool feature." 
      Version="2.0.0.0"
      Scope="Site" 
      Hidden="FALSE"
      DefaultResourceFile="core"
      xmlns="https://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="Elements.xml" />
        <ElementManifest Location="Elements2.xml" />
      </ElementManifests>
      <UpgradeActions
        <VersionRange EndVersion="2.0.0.0">
        <ApplyElementManifests>
          <ElementManifest Location="Elements2.xml" />
          </ApplyElementManifests>
        </VersionRange>
      </UpgradeActions>
    </Feature>
    

When you activate the version "2" Feature, all elements in both the Elements.xml and Elements2.xml files will be provisioned. When you run upgrade on the server farm, the Feature upgrade infrastructure queries for all instances of your Feature that have a version number lower than version 2.0.0.0; and all elements in the Elements2.xml file will be incrementally provisioned for the existing version "1" Feature instances. If no errors occur, the Feature version number is updated in the database. If an error occurs while upgrading a particular instance of the version "1" Feature, that Feature instance is skipped over and the version number is not updated in the database. Subsequent farm upgrades will detect that the Feature instance has not been upgraded and will try to upgrade it again.