Tuesday, September 23, 2008

SPPropertyBag

SPWeb object of SharePoint exposes a Field called Properties which is of type SPPropertyBag a sub class of StringDictionary. It allows you to store Key value String Data within a web

Below are some quick points that will help you in using it.

  • As Mentioned Before it is a StringDictionary, which can only store String values.
  • You need to call the Update() method of the SPWeb.Properties.Update() to persist the value.
  • Same applies while you are removing a value from it.
  • Below code gives you a quick start.
  • If you want to manage this through UI. Refer this link. It's a fantastic option you will love.


 

using (SPSite oSite = new
SPSite("http://mysite"))

{


using (SPWeb oWeb = oSite.RootWeb)

{


//Add a Item to Bag

oWeb.Properties.Add("SPPropertyBag","I can Store a StringValue Here");


//Be sure to Call the Update Method on Properties


//If you miss this your value wont get Presisted for the Next Run

oWeb.Properties.Update();


 


// To Reterive the Value


String sMyItem = oWeb.Properties["SPPropertyBag"];


 


//Remove the Value


//Be sure to Call the Update Method on Properties


//If you miss this your value wont get Presisted for the Next Run

oWeb.Properties.Remove("SPPropertyBag");

oWeb.Properties.Update();

             }

         }

    

    

1 comment:

Anonymous said...

Thank you for the sample.