Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

Six ways to store settings in SharePoint

Posted at 2009-06-09 12:15 by Wictor Wilén in SharePoint with 21 comments.

When developing applications or custom solutions for SharePoint you will on several occasions have to store settings for you application of some kind. When developing database driven or other custom solutions you easily create a database table or make the settings in app/web.config file. You can of course use these approaches when developing for SharePoint, but there are some things to consider when doing this. This post will outline some approaches you can use to store your settings.

What I mean with application or solution settings is some kind of settings that you need for you application to work, such as a Url to a web service or a username and password. These settings are normally only editable by administrators, which requires you to have some security plans, and you need an interface to edit the settings.

Worth to have in mind is that SharePoint can span multiple servers and web applications, it also has the Site Collection and Site scopes which might need different settings if your application runs on several places.

Using a SharePoint List

Creating a database table for storing settings is not the best way in a SharePoint solution - you would use a SharePoint list for this instead. This is the easiest way. Creating a custom list with Title and a Value allows you to store all your settings in one place. There is no need to create a user interface, since you use out-of-the-box functionality and you can secure the list by using permissions on it. The drawback is you need some information (read other setting) that tells you where this list is located, so you're back on square one.

Using web.config

Using web.config is another way to store your settings. It's easy to store information in the appSettings element using a key/value combo. The .NET Framework have built-in functionality to read these settings and SharePoint has some (not so good working) features to deploy the settings in your farm with the SPWebConfigModification class. This approach can only be applied if your settings are web-application wide and you either deploy the settings with the SharePoint object model, which can be quite cumbersome, or manually edit the web.config file on all WFE's.

Using the SPWeb object

The SPWeb object has a Properties property that is persistable StringDictionary stored on the Site (SPWeb) level. This is a perfect solution when you have to store Site specific settings and have the setting available over all your farm. The Properties property is used like this:

   1: SPWeb  web = ...;
   2: // set the value
   3: web.Properties["TheSetting"] = "the value";
   4: // update the value
   5: web.Properties.Update();
   6: // get the value
   7: string val = web.Properties["TheSetting"];

Easy and simple when you have to store key/value combinations, but not so good when you have to store more complex objects, then you have to make some Xml serialization/deseralization yourself.

The permissions in this case is controlled by the MangedWeb permission and you have to build your custom interface.

Using the SPSite object

The SPSite object does not have any Properties property like the SPWeb object, so if you have to store your settings on the Site Collection level you just use the SPSite.RootWeb SPWeb object and use the approach above.

Using SPWebApplication Properties

If you need to store your settings on the Web Application level and you don't to make changes to the web.config, then you can use the SPWebApplication object. The SPWebApplication object inherits from the SPPersistedObject which has a Properties property. This property is a HashTable so you can store key/value combos here. The value can be of any type, as long as it can be serialized.

   1: SPWebApplication webApp = ...
   2: object obj = ...
   3: // set value
   4: webApp.Add("TheSetting", obj);
   5: // persist it
   6: webApp.Update();

This approach requires the user to be farm administrator and you have to build your own interface.

Using custom SPPersistedObjects

Perhaps the best (imho) way to store settings for your web application is to use custom SPPersistedObjects. This approach is very handy when you have to store multiple settings and more complex values. You create your own class that derives from the SPPersistedObject and add your settings as properties to the class and marks them with the Persisted attribute.

   1: public class TheSettings: SPPersistedObject {
   2:     public TheSettings() {}
   3:  
   4:     public TheSettings(string name, SPPersistedObject parent, Guid id) 
   5:         : base(name, parent, id) { }
   6:     
   7:     [Persisted]
   8:     public string WebServiceUrl;
   9: }

Then you use the SPWebApplication GetChild method to retrieve your settings.

   1: TheSettings settigs = webApplication
   2:     .GetChild<TheSettings>("theSettings");

I usually add two static methods to my settings class for easy creating and retrieval of settings:

   1: public static TheSettings GetSettings(SPWebApplication webApplication) {
   2:     TheSettings settings = webApplication.GetChild<TheSettings>("theSettings");
   3:     if(settings == null) {
   4:         return TheSettings.CreateNew(webApplication);
   5:     }
   6:     return settings;
   7: }
   8: public static TheSettings CreateNew(SPWebApplication webApplication) {
   9:    return new TheSettings("theSettings",  webApplication, Guid.NewGuid());
  10: }

This also requires farm administrator to persist and you have to create an interface

That's it

There you have it; six different ways to store and retrieve settings for you SharePoint custom application. When to use which one is up to you and your needs.

What are you preferred ways or do you have any other suggestions or additions?

Comments and trackbacks

#  Environment Configuration approaches by Trackback
Screenshot from websnpr Web.Config Pro \\ Con \\ ASP.NET devs familiar with it Multiple Web Apps means multiple changes Can leverage SPWebConfigModification Can lead to inconsistent web....
#  Config Store framework for storing values in a list by Chris OBrien
Screenshot from websnpr Hi Wictor, Since it seems a common requirement, I built my 'Config Store' framework which makes it easy to store values in a list and provides an API with caching for retrieval. I've used it in a few projects now - I store more complex values as XML (e.g. serialized classes, hierarchical settings etc.). More details at http://spconfigstore.codeplex.com Cheers, Chris.
#  SharePoint Development Weekly Roundup (09Jun) by Trackback
Screenshot from websnpr This week has been action packed with posts! Six ways to store settings in SharePoint by Wicton Wilen
#  Store it in Feature folder by Peter
Screenshot from websnpr Option 7: put an XML file in the Feature folder. This is by necessity not user-editable, and has the benefit of not being reliant on SharePoint itself.
#  @Chris by Wictor
Screenshot from websnpr Great solution Chris. I'll have that in mind for future projects.
#  @Peter by Wictor
Screenshot from websnpr Thanks Peter, thats another option which is good when you have quite static values. Making a UI for that XML file makes you take care of file permissions as well as deployment to other farms. Otherwise you have to redploy your feature.
#  For list speicific settings, use SPList.RootFolder.Properties by Ethan Deng
Screenshot from websnpr To store list specific settings, it is better to use SPList.RootFolder.Properties
#  @Ethan by Wictor
Screenshot from websnpr Yes, that's another one. Thanks
#  Store it in webpart by Nathan Xu
Screenshot from websnpr Option 8: We can use [Personalizable(PersonalizationScope.Shared), WebPartStorage(Storage.Shared)] public string title { } to store the webpart configuration values.
#  @Nathan by Wictor
Screenshot from websnpr Yup, that's also a way to store settings, only that the setting is tied to that WebPart on that page.
#  Store it in webpart by Nathan Xu
Screenshot from websnpr Option 8: We can use [Personalizable(PersonalizationScope.Shared), WebPartStorage(Storage.Shared)] public string title { } to store the webpart configuration values.
#  Environment Configuration approaches by Trackback
Screenshot from websnpr Web.Config Pro \\ Con \\ ASP.NET devs familiar with it Multiple Web Apps means multiple changes Can leverage SPWebConfigModification Can lead to inconsistent web....
#  Environment Configuration approaches by Trackback
Screenshot from websnpr Web.Config Pro \\ Con \\ ASP.NET devs familiar with it Multiple Web Apps means multiple changes Can leverage SPWebConfigModification Can lead to inconsistent web....
#  Resource files too by russell
Screenshot from websnpr You can also store feature specific settings in a features-specific resource file.
#  Resource files too by russell
Screenshot from websnpr You can also store feature specific settings in a features-specific resource file.
#  Thanks ! by GuiSim
Screenshot from websnpr Just wanted to thank you for this. I think you missed webApp._Properties_.Add() the webApp itself doesn't have a "Add" method.
#  Introduction to Property Bags in SharePoint by Trackback
Screenshot from websnpr Introduction to Property Bags in SharePoint
#  Introduction to Property Bags in SharePoint by Trackback
Screenshot from websnpr Introduction to Property Bags in SharePoint
#  Introduction to Property Bags in SharePoint by Trackback
Screenshot from websnpr Introduction to Property Bags in SharePoint
#  Introduction to Property Bags in SharePoint by Trackback
Screenshot from websnpr Introduction to Property Bags in SharePoint
#  Introdução aos Property Bags no Sharepoint by Trackback
Screenshot from websnpr DEFINIÇÕES   Para quem não está familiarizado com os P ...
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.