Thursday, 8 September 2011

Feature.xml and the <MapFile> section; locating files to a different location on the front-end Web server

At some point you will have to upgrade your event receiver, web part, ribbon etc. and in some cases you will have to upgrade files, or you will need to add some.

The section <UpgradeActions> will provide us the fastest way of doing it, At the end of the day we only need to edit the Feature.Xml file and add the section. If you double click in the feature a screen like this will pop up (see below), just click on “(Open in XML Editor)” and you will have a nice Feature.xml to play around.


If we add the <UpgradeActions> code that come in the MSDN documentation ( we should have something like this:

  1: <?xml version="1.0" encoding="utf-8" ?>
  2: <Feature xmlns="">
  3:   <UpgradeActions 
  4:       ReceiverAssembly="MyFeatureReceiver, 
  5:       Version=, 
  6:       Culture=neutral, 
  7:       PublicKeyToken=2f2197d99d6e2871" 
  8:       ReceiverClass="FeatureReceiver.TestFeatureReceiver">
  9:       <CustomUpgradeAction 
 10:         Name="DeleteField">
 11:         <Parameters>
 12:           <Parameter 
 13:             Name="FieldName">Address3
 14:           </Parameter>
 15:         </Parameters>
 16:        </CustomUpgradeAction>
 17:        <VersionRange 
 18:         BeginVersion="" 
 19:          EndVersion="">
 20:           <!-- Here you specify other upgrade actions to apply to Feature instances whose versions are within the range to -->
 21:        </VersionRange>
 22:     <MapFile FromPath=”oldname.gif” ToPath=”newname.gif” />
 23:   </UpgradeActions>
 24: </Feature>

Have a look to line 22, what we are doing here it is renaming files, but you can also move them. If for example we have a folder called “images” in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\MyFeature\images\netsourcecode.gif we can call <MapFile> with the following path:

  1: <MapFile FromPath=”images\netsourcecode.gif” ToPath=”Images\newnetsourcecode.gif” />

or just move it to another folder (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\MyFeature\Gifs\) so we will have

  1: <MapFile FromPath=”images\netsourcecode.gif” ToPath=”Gifs\newnetsourcecode.gif” />

Conclusion: As you can see, sometimes we try to find the answer in the code, but in this case this text file Feature.xml will bring us the fastest way to rename/move a file.

No comments: