in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Roni's SharePoint Thoughts

A developer blog about SharePoint technologies (MOSS and WSS)

MOSS 2007 – Using Property Bag of SPWeb to store Metadata

On an SPWeb there is no out-of-the-box solution to store custom metadata, you only have the name (URL), the title and the description. If you need more (e.g. status (active/inactive) or location) you have to implement a custom solution.

A possible way is to create a list (with the fields you need) that has only one list entry: the metadata of the parent SPWeb. With versioning enabled you also versioned metadata for your SPWeb. But if you have many sites of this type this may be some overkill to have an extra list for the metadata in each site.

Another solution is to use the property bag of the SPWeb and store the additional metadata directly in the SPWeb. This needs some coding of course (Web Part or Layouts application) and you have to integrate it into your sites. But the code is very simple:

Writing values:

// attention: SPWeb.Properties.Remove(string pKey) doesn't work to remove
// a property. To remove a property clear the content.

string strKey = "MyKey";
string strValue = "MyValue";

if (webCurrent.Properties.ContainsKey(strKey))
    // property exists already -> update it
    webCurrent.Properties[strKey] = strValue;
else if (strValue.Length > 0)
    // property doesn't exist -> add it if there is value to set
    webCurrent.Properties.Add(strKey, strValue);
webCurrent.Properties.Update();

Reading values:

string strKey = "MyKey";
string strValue = string.Empty;

if (webCurrent.Properties.ContainsKey(strKey))
    strValue = webCurrent.Properties[strKey];

If you want to save some metadata about the lists in the current SPWeb you can also use the property bag of your SPWeb. Just add the Guid of the list to each name of the property:

string strKey = "MyKey_" + listCurrent.ID.ToString();

Published Aug 27 2007, 04:58 PM by rho

Comments

 

Cédric said:

Hello Roni,

This solution is really nice but I have one problem with it :

Once my metadata are added to my SPWeb properties, it's impossible to remove them.

Here is my code to delete :

site.Properties.Remove("mykey");

site.Properties.Update();

site.Update();

Just after this, if I read the content of site.Properties["mykey"], it returns nothing (normal). But if I comment the 3 lines above and refresh my page, site.Properties["mykey"] contains the value I've added before (strange).

Any Idea ?

August 27, 2007 10:14
 

rho said:

Hello Cédric, I've the same behaviour. I think the Remove method doesn't work (bug). You can only clear the content (set it to string.Empty). See also the comment line at the beginning of my code.

August 27, 2007 10:26
 

Cédric said:

It works, thanks for your quick answer.

August 28, 2007 1:42
 

10 Links Today (2007-08-28) said:

Pingback from  10 Links Today (2007-08-28)

August 28, 2007 10:19
 

Pete said:

Any idea how well that is integrated with search? Can users search for sites using the regular search screens, or would one need to create a new search and code it to look in the property bags?

August 28, 2007 4:09 PM
 

rho said:

I think, by default properties in the property bag won't be indexed.

To get them indexed you have to display the values on a page (e.g. in a WebPart). Or add them (programmatically) to the metatags of a page.

August 30, 2007 10:30
 

Tippu (Mubarak) said:

SPWeb.Properties.Remove(string pKey) doesn't work to remove a property. To remove a property use the following lines:

webCurrent.Properties[strKey] = null;

webCurrent.Properties.Update();

webCurrent.Update();

-- Tippu

April 10, 2008 2:20
 

Parthi said:

I was in a similar situation and figured it out myself..

I am using webCurrent.AllProperties and update works well fine for me.

webCurrent.AllProperties.Remove("MyKey");

webCurrent.Update();

I didn't try to remove existing proerties as that might break the site, but it works fine for my meta-data

I never tried this for webCurrent.Properties though...

April 18, 2008 3:16 PM
 

Jarret Franklin said:

WOW! I have been fighting with this for hours! I couldn't figure out why the property persisted after I called Remove()!

Setting the property to NULL and then updating both the properties and web did the trick for me...

April 23, 2008 2:01 PM
 

Stephen Kaye said:

The answer is to set the property to null before calling remove then you only need to call update on the properties not the web

E.g.

 web.Properties["MyProperty"] = null;

 web.Properties.Remove("MyProperty");

 web.Properties.Update();

May 15, 2008 4:35
 

Stephen Kaye said:

Sorry I think I've fallen foul to the same issue as Jarret. The properties appear to have been removed but if you recreate you SPWeb object you'll see that they've come back

May 15, 2008 4:42

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts