Using a Feature Receiver to Create a Configuration Setting
Typical Goals
When you deploy an application to a SharePoint environment, you will often need to ensure that configuration settings are both specified and available before your application is first run. For example, suppose that you have developed a Web Part that provides a map view of customer locations, based on geo-coded data from a SharePoint list. Before your Web Part can do its job, it needs to know where to find the customer location list.
Solution
The Application Setting Manager exposes an interface named IConfigManager. This interface defines methods that you can use to add and update application settings. To ensure that the required settings are available before the Web Part is added to a page, you can use a feature receiver class to add the configuration settings when the Web Part feature is activated.
Using the IConfigManager Interface to Add Application Settings
The following code shows how to use the IConfigManager interface to add an application setting from within a feature receiver class.
This example assumes that you have added a reference to the Microsoft.Practices.SharePoint.Common.dll assembly, the Microsoft.Practices.ServiceLocation.dll assembly, and the Microsoft.SharePoint.dll assembly.
using Microsoft.SharePoint;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Configuration;
[Guid("8b0f085e-72a0-4d9f-ac74-0038dc0f6dd5")]
public class MyFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
IConfigManager configManager =
serviceLocator.GetInstance<IConfigManager>();
SPSite mySite = properties.Feature.Parent as SPSite;
if(mySite != null)
{
configManager.SetWeb(mySite.RootWeb);
IPropertyBag bag = configManager.GetPropertyBag(
ConfigLevel.CurrentSPSite);
configManager.SetInPropertyBag(
"Contoso.Sales.Applications.CustomerLocationsListUrl",
"http://intranet.contoso.com/sites/sales/CustomerLocations",
bag);
}
}
}
Note
The Parent property of a feature depends on the scope of the feature that is being activated. In this example, the feature is scoped at the site collection level.
For more information about using the IConfigManager interface to add or update configuration settings, see Adding and Updating Application Settings.
Usage Notes
As a good practice, consider removing any related application settings from the SharePoint environment when you deactivate a feature. You can do this by overriding the FeatureDeactivating method in a feature receiver class. However, remove configuration settings only if you are certain that no other features are using the configuration data, and take care to avoid removing settings that other instances of the activated feature may rely on. Creating the application settings at the same scope as the application feature will typically avoid this situation.
Adding, updating, and removing configuration settings can lead to contention issues, because only one process can write to a property bag at any one time. If SharePoint is unable to write to a property bag because of contention, it will throw an SPUpdatedConcurrencyException. The SPFarm and SPWebApplication property bag implementations will automatically retry the operation in this situation, but the retry limit may be exceeded in heavy contention situations. In this case, the ConfigManager class will throw a ConfigurationException, and the inner exception will be an SPUpdatedConcurrencyException.