Partager via


Feature properties, web.config, and feature properties inheritance

My team ran into this last week where I asked to have some properties available to the feature.  It was the first time that we were allowing properties in the features and I hadn't given much explanation as to how I was seeing the property definition.  They are all experienced ASP.NET web developers so the first thing that popped into mind was to use the web.config to store properties.

 

While this is a common practice in ASP.NET, experienced SharePoint feature or site definition makers will know that there is also a way to use properties in the Xml directly but I'll get back to you.  First, let's handle the web.config; will it work? Well it will work when your feature is activated through your web site's interface.  However, when you use an STSADM to create Web,the web.config will not be in the same process and your feature will not work.

 

So how do we do it, we use <Properties> and <Property Key=... Value=...> xml elements:

<Feature
  Id="11111111-1111-1111-1111-11111111111"
  Title="Location Services"
  Description="This Feature contains lists and parts that let you link location data to your customer lists."
  Scope="Web">
  <ActivationDependencies>
    <ActivationDependency
      FeatureId="11111111-1111-1111-1111-111111111111" />
  </ActivationDependencies>
  <Properties>
<Property
Key="Color"
Value="Blue"/>
<Property
Key="Shape"
Value="Triangle"/>
</Properties>
</Feature>

 

In your feature event, you can then retrieve the feature's property values by "properties.Feature.Properties" and this will return an SPFeaturePropertyCollection object with your properties.  An interesting fact is that you can use the "Properties" element in 2 places : in the feature.xml itself and in the onet.xml (site definition).  How it works is that your SPFeaturePropertyCollection will contain the sum of all properties defined at the 2 areas.  If a property's defined at both areas, it will use the value in the onet.xml file.

 

Note that you cannot set property values in the ActivationDependency element.  While activate dependencies will activate a feature if it's hidden, it's still just a mechanism to enforce a dependency.  If you depend on a non-hidden feature, it requires to be activated prior to activating yours.

 

Maxime

Comments