How to add and read properties from the feature.xml

1 minute read

I knew it is was possible to provide features with properties but I never found a useful situation till a few days ago.

So know is the question how do you add properties to your feature.xml? You can accomplish this by adding a properties tag to the feature.

 <?xml version="1.0" encoding="utf-8" ?>
 <Feature Id="FeatureID"
              Title="FeaturePropertiesExample"
              Description="Example for working with feature properties"
     Version="1.0.0.1"
     Scope="Web"
     Hidden="FALSE"
     ReceiverAssembly="Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=Token"
              ReceiverClass="The Receiver class"
     xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests />
     <Properties>
         <Property Key ="Variation" Value="Dutch" />
         <Property Key ="VariationLabel" Value="nl-NL" />
     </Properties>
 </Feature>

In this example I saved the variation label I want to add to the variation.

If you would Activate the feature you can read out the Property Variation and Variation Label like this:

/// <summary>
 /// Feature Activation
 /// </summary>
 ///<param name="properties">Feature properties</param>
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
 {
    //Get property collection
    SPFeaturePropertyCollection featureProperties = properties.Feature.Properties;

    //Get seperate properties with the key
    string variationDisplayName = feautureProperties[Variation].toString();
    string variationLabel= feautureProperties[VariationLabel].toString();
 }