Tuesday, April 21, 2009

Using SPPropertybag to save value

Hi,
this small code snippet will explain how to store value to Sharepoint proerty bag and how to fetch it.

How to save value to SPProperty Bag
Here we will create an instanceof SPPropertyBag and try to add a key 'BlogImage' to the bag:

SPPropertyBag spProperties = SPContext.Current.Web.Properties;
if (spProperties.ContainsKey("BlogImage")) spProperties.Remove("BlogImage");
spProperties.Add("BlogImage", path);
spProperties.Update();
SPContext.Current.Web.Update();

NOTE: Updating SPPropertybag and SPWeb is important.

How to get value from SPProperty Bag

SPPropertyBag spProperties = ospWeb.Properties;
string blogImageValue=string.Empty;
if (spProperties["BlogImage"] != null && spProperties["BlogImage"] != "")
{
blogImageValue= spProperties["BlogImage"].Trim();
}

No comments: