CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Working with web.Properties

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):

 

 

To remove just run handle as every Dictionary:

web.Properties.Remove(key);
web.Properties.Update()

Or:

$web.Properties.Remove("my-key");
$web.Properties.Update()

The same we can do with web application, just here you don’t need to set AllowUnsafeUpdates to true, neither it won’t be neccessary to run webapp.Properties.Update, just update webapp:

$webapp = get-spwebapplication "http://contoso"
$webapp.Properties.Add("hello", "world")
$webapp.Update()
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

%d bloggers like this: