Working with web.Properties
By Anatoly Mironov
Sometimes one may need more properties to track on a SPWeb beside Title and Description. One of the possibilities is to create a custom list (maybe a hidden one) with keys and values (Title and Value). It works fine. The good thing with it is also the possibility to change the key-value pair directly in the web interface. Another approach is to use web.Properties which is a Dictionary with key-values pairs. A simpler and neater solution: Here is a good motivation from the best sharepoint book Sharepoint as a Development Platform (p. 1043):
Each site includes a property bag, which is widely used internally by SharePoint. However, there is no reason why you cannot also use this property bag to store common settings related to a site. You can regard the bag as an application settings collection, equivalent to what you would normally define in a web.config or app.config file. As in the configuration files, the settings are a collection of key/value pairs.
To be able to distinguish your values from standard Sharepoint use a prefix. Here are two methods: one for writing a property, and one for listing all properties:
const string PREFIX = "contoso\_";
private void AddEntry(string key, string value)
{
using (SPSite site = new SPSite("http://contoso/"))
{
using (SPWeb web = site.RootWeb)
{
key = PREFIX + key;
if (!web.Properties.ContainsKey(key))
{
web.Properties.Add(key, value);
web.AllowUnsafeUpdates = true;
web.Properties.Update();
}
}
}
}
private void ListEntries()
{
using (SPSite site = new SPSite("http://contoso/"))
{
using (SPWeb web = site.RootWeb)
{
foreach (DictionaryEntry entry in web.Properties)
{
Console.WriteLine("{0} = {1}", entry.Key, entry.Value);
}
}
}
}
```And of course, all this you can do in Powershell (even a response to shorcoming to not be able to change in UI): [![](https://sharepointkunskap.files.wordpress.com/2011/10/web-prop-ps.png "web-prop-ps")](https://sharepointkunskap.files.wordpress.com/2011/10/web-prop-ps.png) [![](https://sharepointkunskap.files.wordpress.com/2011/10/web-prop-ps-001.png "web-prop-ps-001")](https://sharepointkunskap.files.wordpress.com/2011/10/web-prop-ps-001.png) To remove just run handle as every Dictionary:
web.Properties.Remove(key); web.Properties.Update()
$web.Properties.Remove(“my-key”); $web.Properties.Update()
$webapp = get-spwebapplication “http://contoso” $webapp.Properties.Add(“hello”, “world”) $webapp.Update()