Understanding SharePoint Property Bag Settings

Today my ex-coworker and friend ask me regarding SharePoint property bag, therefore I got an opportunity of posting this article on understanding SharePoint property bag settings.

SharePoint property bag is a good place where we are able to accumulate key and value like a couple, It’s a hash table.

There is no such user interface to view/add/edit/delete the property bag key-value, but there is an option to perform the actions on property bag via SharePoint designer.

After you open a site in SharePoint Designer, go to Site menu click on Settings option, a Site Settings dialog box opens, click on Parameters tab where you can see the list of existing properties, from the same place you can even perform add/modify/remove actions.

View SharePoint property bag properties using SharePoint Designer

View SharePoint property bag properties using SharePoint Designer

Subsequently, prior to start on deep into property bag, we will discuss going on additional area some where we can store up value other than SharePoint property bag.

web.config file is too a place where we be able to accumulate the settings such as file location, URLs, etc.
Example:

[sourcecode language=”xml”]





[/sourcecode]

In view of the fact that we have single global web.config file meant for complete site. If we would like to accumulate additional settings correlated to several sites we have to make multiple entries in web.config file.

One more method of storing the settings by creating table in database, this approach is not recommend all the time in SharePoint.

Even we can store values in SharePoint list by creating two columns like Keys and Values using OOTB functionality and still can access the key-value pair using object model.

But, if you have several settings related to different sites with some unique permissions for individuals sites then we have to decide where the list is to be created.

On the way to over come this situation SharePoint provided property bag to store the properties in several levels like SPFarm, SPWebApplication, SPSite, SPWeb and SPList.

Storing key-value in SPFarm include more danger that require farm administrator privileges.

Below are few examples on SPFarm, SPWebApplication, SPSite, SPWeb add/view/edit/delete property bag values.

Add property in SPFarm level

[sourcecode language=”csharp”]
static void Main(string[] args)
{
SPFarm myFarm = SPFarm.Local;
myFarm.Properties.Add(“SPFarmKey”, “SPFarmValue”);
myFarm.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

View SPFarm property

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SSPFarm myFarm = SPFarm.Local;
if (myFarm.Properties != null && myFarm.Properties.Count > 0)
{
if (myFarm.Properties.ContainsKey(“SPFarmKey”))
{
Console.WriteLine(myWebApplication.Properties[“SPFarmKey”]);
}
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}

[/sourcecode]

Modify property in SPFarm level

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPFarm myFarm = SPFarm.Local;
myFarm.Properties[“SPFarmKey”] = “NewSPFarmValue”;
myFarm.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Delete property in SPFarm level

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPFarm myFarm = SPFarm.Local;
myFarm.Properties[“SPFarmKey”] = null;
myFarm.Properties.Remove(“SPFarmKey”);
myFarm.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Add property in SPWebApplication level

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri(“http://WebApplicationURL”));
myWebApplication.Properties.Add(“SPWebAppKey”, “SPWebAppValue”);
myWebApplication.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

View SPWebApplication property

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri(“http://WebApplicationURL”));
if (myWebApplication.Properties != null && myWebApplication.Properties.Count > 0)
{
if (myWebApplication.Properties.ContainsKey(“SPWebAppKey”))
{
Console.WriteLine(myWebApplication.Properties[“SPWebAppKey”]);
}
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}

[/sourcecode]

Modify property in SPWebApplication level

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri(“http://WebApplicationURL”));
myWebApplication.Properties[“SPWebAppKey”] = “NewSPWebAppValue”;
myWebApplication.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Delete property in SPWebApplication level

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri(“http://WebApplicationURL”));
myWebApplication.Properties[“WebAppKey”] = null;
myWebApplication.Properties.Remove(“WebAppKey”);
myWebApplication.Update();
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

There is no facility to store properties in SPSite object similar to SPWeb object, if you want to store the settings in site collection level we have to use SPSite.RootWeb

Add/retrieve SPSite property

[sourcecode language=”csharp”]

static void Main(string[] args)
{
SPSite siteCollection = new SPSite(“http://servername:port”);
SPWeb site = siteCollection.RootWeb;
//Addning SPSite Property
site.Properties.Add(“SPSiteKey”, “SPSiteValue”);
site.Properties.Update();

//Reading SPSite Property
Console.WriteLine(site.Properties[“SPSiteKey”]);
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Add/retrieve SPWeb property

[sourcecode language=”csharp”]
static void Main(string[] args)
{
SPSite mySite = new SPSite(“http://servername:port”);
SPWeb myWeb = mySite.OpenWeb(“Your Web Name…..”);
//Adding SPWeb Property
myWeb.Properties.Add(“SPWebKey”, “SPWebValue”);
myWeb.Properties.Update();

//Reading SPWeb Property
Console.WriteLine(myWeb.Properties[“SPWebKey”]);
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]