Settings in Sharepoint_config database instead of appSettings
By Anatoly Mironov
An alternative to store key-value paired properties in appSettings in web.config we can use Sharepoint_config database by creating our own SPPersistedObject. First we have to create a class which inherits from SPPersistedObject and decorate its properties with [Persisted]:
namespace Takana.SharePoint
{
public class Settings: SPPersistedObject
{
\[Persisted\]
public string DbConString;
public LinkCheckerPersistedSettings() { }
public LinkCheckerPersistedSettings(string name, SPPersistedObject parent) : base(name, parent) { }
}
}
```Then we must create such an object. Let's do it in Powershell:
if(-not(Get-PSSnapin | Where { $_.Name -eq “Microsoft.SharePoint.PowerShell”})) { Add-PSSnapin Microsoft.SharePoint.PowerShell }
[Reflection.Assembly]::LoadWithPartialName(“takana.sharepoint”)
$app = get-spwebapplication http://takana
$configName = “takana.sharepoint.settings”
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$settings = $farm.GetObject($configName, $app.Id, [takana.sharepoint.settings])
if ($settings -ne $null) { $settings.Delete() }
#Create a new persisted settings object
$settings = New-Object Takana.Sharepoint.settings ($configName, $app)
$settings.DbConString = “Some Connection String”
$settings.Update();
public static string FindSettings(string key, SPWebApplication app) { var s = string.Empty; try { var farm = Microsoft.SharePoint.Administration.SPFarm.Local; var settings = farm.GetObject(“Takana.Sharepoint.Settings”, app.Id, typeof(Settings)) as Settings; if (settings != null) { var type = typeof(Takana.SharePoint.Settings); var f = type.GetField(key); s = f.GetValue(settings) as string; } } catch (ArgumentException ex) { throw new ArgumentException(“Settings could not be found.”), ex); } return s; }
## Comments from Wordpress.com
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2012-01-23 11:29:47">Jan 1, 2012</time>
Thank you Jens for your input! The property bag of SPWeb or SPWebApplication are handy, indeed. [I have used them a lot](/2011/10/25/working-with-web-properties/ "see my blog post about them"). Using SPPersistedObject is tricky and when possible should be avoided. I tested this method, and this blog post is just a brain dump :) Maybe it can be useful in the future. By the way, in powershell, you can a reference to your web directly: $web = get-spweb "http://server"
<hr />
####
[Jens Malmberg]( "jens.malmberg@gmail.com") - <time datetime="2012-01-23 10:59:57">Jan 1, 2012</time>
Thank you for a good blogg! ;) Very useful. I can also recommend using the property bag, which is a key-value set which you can set on farm, application, site or web level. usable via code: //Retrieve a property entry web.AllProperties\[key\] // Add a property entry web.AllProperties\[key\] = value; web.Update(); // Remove a property entry web.AllProperties.Remove(key); web.Update(); via powershell: $site = spsite "http://server" $web = $site.OpenWeb() $web.AllProperties I for one use it in a current project where we set up a custom configuration site in central admin where we store and fetch settings from. Using: SPAdministrationWebApplication.Local.Properties\["importPersonalRoot"\].ToString(); to fetch settings at run time. /Jens
<hr />