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.

image

If we add the <UpgradeActions> code that come in the MSDN documentation (http://msdn.microsoft.com/en-us/library/ee537575.aspx) we should have something like this:

  1: <?xml version="1.0" encoding="utf-8" ?>
  2: <Feature xmlns="http://schemas.microsoft.com/sharepoint/">
  3:   <UpgradeActions 
  4:       ReceiverAssembly="MyFeatureReceiver, 
  5:       Version=1.0.0.0, 
  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="2.0.0.0" 
 19:          EndVersion="5.0.0.0">
 20:           <!-- Here you specify other upgrade actions to apply to Feature instances whose versions are within the range 2.0.0.0 to 5.0.0.0 -->
 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: