Utiliser la mise à niveau des fonctionnalités pour appliquer de SharePoint pages maîtres lors de la mise à niveau SharePoint 2010
Découvrez comment réinitialiser une page maître personnalisée pendant l’événement lors de la mise à niveau d’un site SharePoint du mode de compatibilité FeatureUpgrading 2010 vers le mode de compatibilité 2013.
Lorsque vous mettez à niveau vos personnalisations SharePoint 2010 vers SharePoint, toutes les références aux pages maîtres personnalisées que vous avez créées reviennent à la page default.master. Si vous avez déployé une fonctionnalité qui contient une ou plusieurs pages maîtres personnalisées vers une collection de sites SharePoint mise à niveau qui s’exécute en mode de compatibilité SharePoint 2010, vous devrez réinitialiser vos pages maîtres SharePoint lorsque vous passerez au mode de compatibilité 2013. Cette rubrique explique comment utiliser un récepteur de fonctionnalités pour vous assurer que vos pages maîtres personnalisées SharePoint sont réinitialisées lors de la mise à niveau depuis SharePoint 2010.
Use custom upgrade code to reset a master page
Comme l’expliquent les instructions de Déploiement de fonctionnalités personnalisées vers des collections de sites mises à niveau dans SharePoint, lorsque vous faites une mise à niveau de SharePoint 2010 vers SharePoint, les collections de sites de votre batterie de serveurs s’exécuteront en mode de compatibilité SharePoint 2010 par défaut. Depending on the approach to upgrading your custom features that you've taken, you'll have deployed any given feature by using either one or two solution packages:
- A single solution package that can be deployed to both the "14" and the "15" compatibility levels, either because of custom logic for the "15" compatibility level, or because the feature works without any problems in both the "14" and the "15" compatibility levels.
- Two solution packages that contain different versions of the same feature. This approach is called "feature masking."
In either case, during upgrade any custom master pages you've created will revert to the default.master page. If you don't reset these pages with logic inside your feature, you'll need to reactivate the feature (or the "15" version of the feature) to reset the master pages to your custom versions. You can reset your 2013 custom master pages by using a feature receiver that is tied to the FeatureUpgrading event.
To reset a 2013 custom master page with a feature receiver
Open your solution in Visual Studio. Find your feature under the Features node in Solution Explorer, and open the feature.xml file for your feature.
Add an
<UpgradeActions>section to the feature.xml file and make sure that the action applies only to the version of the feature that is currently in use for the "14" compatibility level. This section specifies the name of an action to perform when the feature is upgraded. The following example specifies an upgrade when version 1.0.0.0 of the feature is in use. In the example, the actionUpgradeFeatureis passed to the implementation of the FeatureUpgrading(SPFeatureReceiverProperties, String, IDictionary<String, String>) method that you'll define later after you've added a feature receiver.<UpgradeActions ReceiverAssembly="MyFeatureReceiver, Version=2.0.0.0, Culture=neutral, PublicKeyToken=<token>" ReceiverClass="MyFeature.MyFeatureEventReceiver"> <VersionRange BeginVersion="1.0.0.0" EndVersion="1.0.0.0"> <CustomUpgradeAction Name="UpgradeFeature"/> <ApplyElementManifests> <ElementManifest Location="MasterPages\\UpgradeElements.xml" /> </ApplyElementManifests> </VersionRange> </UpgradeActions>You place the master page or pages in the MasterPages folder of the project, and any metadata related to the master page(s) in the UpgradeElements.xml file.
Add a
<Properties>section to the feature.xml file. This section contains key-value pairs that specify the 2013 custom master page or pages that you want to set when the site is upgraded. The following example specifies the value of theMy15MasterPagekey that you'll use in the feature receiver.<Properties> <Property Key="My15MasterPage" Value="_catalogs/masterpage/My15MasterPage.master" /> </Properties>In Solution Explorer, under the Features node, right click the name of your feature, and then choose Add Event Receiver to add an event receiver to the feature.
This adds a code file under your feature in Solution Explorer. Figure 1 shows where a sample Feature1.EventReceiver.cs file appears under the feature in the Features folder.
Figure 1. The code file for an event receiver in a feature

This file contains a commented and empty
FeatureUpgradingmethod. You'll use this method in the following step.Open the code file and uncomment the FeatureUpgrading method, which overrides the FeatureUpgrading(SPFeatureReceiverProperties, String, IDictionary<String, String>) method. The following example applies the
My15MasterPagefile that was specified earlier in the feature.xml file.public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters) { try { if (upgradeActionName != "UpgradeFeature") return; //Set the master page to a value stored as a property in the feature.xml file string masterPage = properties.Definition.Properties[My15MasterPage].Value; string baseURL; var currentWeb = properties.Feature.Parent as SPWeb; //Checks to see that the API returns a string that ends in a "/" and if not adds it. if (currentWeb.ServerRelativeUrl.Substring(currentWeb.ServerRelativeUrl.Length - 1) == "/") { baseURL = currentWeb.ServerRelativeUrl; } else { baseURL = currentWeb.ServerRelativeUrl + "/"; } masterPage = baseURL + masterPage; currentWeb.CustomMasterUrl = masterPage; currentWeb.Properties.Update(); currentWeb.Update(); } } catch (Exception ex) { //Handle exception } }
Once you're done with the upgrade, you'll want to think about the future and long-term maintenance of your feature. Reportez-vous à La Gestion du cycle de vie des applications SharePoint 2010 pour obtenir des instructions sur la gestion du code de confiance totale. Bien que cet article se rapporte SharePoint 2010 spécifiquement, il s’applique également bien au code de confiance totale dans SharePoint. If you aren't familiar with feature versioning and upgrade actions, refer to the Models for Solution Lifecycle Management section of this article. You should also look at Best Practices for Using Feature Versions.
Voir aussi
- Déployer des fonctionnalités personnalisées sur des collections de sites mises à niveau dans SharePoint
- Mettre à niveau les personnalisations de site SharePoint
- Mise à niveau vers SharePoint
- SharePoint pack de solutions SharePoint Online pour la branding et la mise en service de site
- Installer et gérer des solutions pour SharePoint