Skip to content

Manage SPWeb propertybag from c# code and SharePoint Designer

September 7, 2011

A little known feature of SharePoint is that it is possible to assign custom properties to each SharePoint site either programmatically or using SharePoint designer. Unfortunately there is no web user interface to view or set these properties, so this feature remains hidden for most SharePoint users.

The properties can be accessed using the following methods:

SharePoint Designer
Open the website in SharePoint designer and navigate in the menu to Sites –> Site Settings –> Parameters.
Use the dialog to view and manage the property values as shown in this image:


The following C# code shows how to set and retrieve the properties. These method extend the SPWeb object, making the management of these properties quite simple. (Actually I’m wondering why these methods are not part of the standard SharePoint API)

	public static class PropertyBagExtension

		public static void AddProperty(this SPWeb web, string key, string value)
			web.AllowUnsafeUpdates = true;
			web.Properties[key] = value;
			web.AllProperties[key] = value;

		public static void RemoveProperty(this SPWeb web, string key)
			web.AllowUnsafeUpdates = true;
			web.Properties[key] = null;

		public static string GetPropertyValue(this SPWeb web, string key)
			if (web.AllProperties.ContainsKey(key))
				return web.AllProperties[key].ToString();

			return null;

Note that the above code updates both the AllProperties and Properties property of the SPWeb. This helps to avoid many issues.
When done, adding a property to the SPWeb is just a single line of code:

	web.AddProperty("myProperty", "my-property-value");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: