How to Save Configuration Data in SharePoint Development

Written by Denis Stadler on . Posted in Custom Development

When we develop custom application over the SharePoint platform, often we need to store different configuration parameters as: connection strings, e-mail addresses, URLs or different data parameters.

In SharePoint we have the following possibilities to store the configuration data (I will enumerate them based on my preference):

SharePoint Property Bag

This can be used for all the custom SharePoint solutions (Farm, Web Application, Site Collection, Web). The only disadvantage that I found is the fact that you have to build an application page or a web part to manage this items, because there is no interface provided by default.

What I wanted to share in this article were the get/set (load/save) methods that I use to access the Property Bag in my custom solutions. Usually I create a public static class called Helpers, where I store all the common methods needed in a solution. To load and save the Property Bag data I created a LoadProperty and a SaveProperty method with two overloads each.

In case we call this method from a FeatureActivated event, we need the second overload. In this case the object SPContext.Current.Web does not exist. Anyway you can find below the code that I used:

#region Load Configuration Property
public static string LoadProperty(string strKeyName)
{
string strResult = string.Empty;

SPWeb webCurrent = SPContext.Current.Web;

if (webCurrent.Properties.ContainsKey(strKeyName))
{
strResult = webCurrent.Properties[strKeyName].ToString();
}
return strResult;
}

public static string LoadProperty(SPWeb webCurrent, 
string strKeyName)
{
string strResult = string.Empty;

if (webCurrent.Properties.ContainsKey(strKeyName))
{
strResult = webCurrent.Properties[strKeyName].ToString();
}
return strResult;
}
#endregion

#region Save Configuration Property
public static void SaveProperty(string strKeyName, 
string strKeyValue)
{
SPWeb webCurrent = SPContext.Current.Web;

if (!webCurrent.Properties.ContainsKey(strKeyName))
{
webCurrent.Properties.Add(strKeyName, strKeyValue);
}
else
{
webCurrent.Properties[strKeyName] = strKeyValue;
}
webCurrent.Properties.Update();
}

public static void SaveProperty(SPWeb webCurrent, 
string strKeyName, string strKeyValue)
{
if (!webCurrent.Properties.ContainsKey(strKeyName))
{
webCurrent.Properties.Add(strKeyName, strKeyValue);
}
else
{
webCurrent.Properties[strKeyName] = strKeyValue;
}
webCurrent.Properties.Update();
}
#endregion

One more thing: if you want to delete a Property Bag item just set its value to null. The Properties collection has a Remove method but it seems that it isn’t implemented, so calling a SPWeb.Properties.Remove won’t produce any effect.

public static void DeleteProperty(string strKeyName)
{

SPWeb webCurrent = SPContext.Current.Web;

try
{
webCurrent.Properties[strKeyName] = null;
}
catch { }
webCurrent.Properties.Update();

}

public static void DeleteProperty(SPWeb webCurrent, 
string strKeyName)
{

try
{
webCurrent.Properties[strKeyName] = null;
}
catch { }
webCurrent.Properties.Update();

}

Web.config

This option is available only for solutions designed as Farm-based solutions, which are IIS-based application.

For example you can’t use web.config to save the configuration date for Timer jobs (they run under the OWSTimer service, which is outside IIS) and, of course, Sandbox Solutions.

Another thing you should be aware of is the fact that the user, under who’s identity this farm solution runs, has to have access to the SharePoint Configuration Database.

Hierarchical Object Store

It can be used for Farm or Web Application solutions. As in the Property Bag option it doesn’t provide a built in interface.

The data is stored in the Configuration Database so it requires elevated permissions.

SharePoint List

The big advantage of using a SharePoint list is the fact that we can use the built in interface to manage the configuration data.

The biggest disadvantage of using this I think is the fact that we have to pay an increased attention when configuring this, not to expose sensitive data like users and passwords.

Tags: , ,

Trackback from your site.

Denis Stadler

I'm a technology enthusiast, with more than 10 years of experience in SharePoint and Dynamics CRM projects. To find more details about, please visit the about me page.

Leave a comment

*