SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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();


Posted 08-27-2007 4:58 PM by rho

Comments

Cédric wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 08-27-2007 10:14 AM

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 ?

rho wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 08-27-2007 10:26 AM

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.

Cédric wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 08-28-2007 1:42 AM

It works, thanks for your quick answer.

10 Links Today (2007-08-28) wrote 10 Links Today (2007-08-28)
on 08-28-2007 10:19 AM

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

Pete wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 08-28-2007 4:09 PM

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?

rho wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 08-30-2007 10:30 AM

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.

Tippu (Mubarak) wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 04-10-2008 2:20 AM

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

Parthi wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 04-18-2008 3:16 PM

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...

Jarret Franklin wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 04-23-2008 2:01 PM

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...

Stephen Kaye wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 05-15-2008 4:35 AM

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();

Stephen Kaye wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 05-15-2008 4:42 AM

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

Tippu (Mubarak) wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 10-14-2008 3:59 AM

I have added an update to this blog entry by adding the code snippet for removing a Property from the Property Bag.

tippu.blog.co.in/.../moss-2007-%E2%80%93-using-property-bag-of-spweb-to-store-metadata

Hope this will be help full for every one

MOSS 2007 ??? Using Property Bag of SPWeb to store Metadata | Tippu's Blog wrote MOSS 2007 ??? Using Property Bag of SPWeb to store Metadata | Tippu's Blog
on 10-19-2008 10:11 PM

Pingback from  MOSS 2007 ??? Using Property Bag of SPWeb to store Metadata | Tippu's Blog

ye wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 11-03-2008 1:36 AM

Its working perfectly

Alon Havivi wrote re: MOSS 2007 – Using Property Bag of SPWeb to store Metadata
on 01-22-2009 2:46 AM

I have created a project (Property Bag Settings) on codeplex that helps you to manage configuration information using the property bag  in SharePoint at all levels:

www.codeplex.com/pbs

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.