Showing posts with label Property bag. Show all posts
Showing posts with label Property bag. Show all posts

Monday, September 3, 2012

What is a Property Bag in sharepoint 2010


In this post we are going to discuss what is Property Bag in sharepoint 2010.

Basically this property bag is a feature available in Windows SharePoint Services 3.0.
It is a Hash Table where in we will maintain key-Value information. The basic fundu of this Property bag is to add the properties to the objects of the SharePoint site.

Why Property bag instead of web.config?
While defining the property bag we said it is an Hash Table where we will maintain Key-Value information. The question arises here is, when we can maintain key-value information in web.config(appsettings section-Ref below appsettings) why should we go for Property bag.

<configuration>
    <appSettings>
        <add key="MyKey" value="MyValue" />
    </appSettings>
</configuration>

And the answer is, When we add key-value in web.config which will become common to the entire web application.

What, if there is any setting specific to individual site in the web application?
In this scenario we will have to adopt either of the below cases.
è        1) Maintain multiple key-value entries in web.config.
è        2) Use sharepoint property bag. With this we can store the properties in several levels like SPFarm, SPWebApplication, SPSite, SPWeb and SPList.

Property Bag in sharepoint 2010:
There is no specific user interface available to add/modify the property bag settings. We have to do it through object model.
Also there is an option available in SharePoint designer to add/modify the property bag settings. Go to Site -> Site Settings. click on the Parameters tab.  On this tab, you will be able to manipulate all of your custom property bag values.

Add/Modify Property bag with Object model:
In the below example we are adding/reading/updating/deleting the properties at the farm level. Similarly we can do it for rest all the scopes mentioned above.

Adding:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties.Add("myFarmKey", "myFarmValue");
myFarm.Update();

Reading:
SSPFarm myFarm = SPFarm.Local;
if (myFarm.Properties != null && myFarm.Properties.Count > 0)
{
if (myFarm.Properties.ContainsKey("myFarmKey"))
{
String myFarmValue= myWebApplication.Properties["myFarmKey"];
}
}

Updating:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = "myNewFarmValue";
myFarm.Update();

Deleting:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = null;
myFarm.Properties.Remove("myFarmKey");
myFarm.Update();


Hope this helps…

Popular Posts