Search This Blog

Friday, October 26, 2012

What is property bag in SharePoint 2010?

Total Answers : 5

Answer 1
Property bag is basically properties attached on web site ,site collection , web application and farm level.
You can check out the codeplex tool here for more understanding - http://pbs2010.codeplex.com/
You also have a list of cmdlets from Powershell for property bag - http://collab.rdacorp.com/2010/05/sharepoint-2010-property-bag-cmdlet.html
Answer 2
Share-Point property bags provide an easy-to-use storage mechanism for any serializable configuration data. Below is the sample code:
SPWeb web = SPContext.Current.Site.RootWeb;

//To store data in property bag

web.Properties["TestKey"]="TestData";

//To retrieve data stored in property bag                 
if (web.Properties.ContainsKey("TestKey"))
{
  string strResult=web.Properties["TestKey"];
}
We must ensure that any data we store in Property Bags is serializable. If you attempt to persist non-serializable types in property bags,the configuration database or the content database may get corrupted. So, it is recommended you use "Application Setting Manager" to store\retrieve values in property bags
Answer 3
One of the uses of SharePoint Manager 2010 (http://spm.codeplex.com/) is to view and update the property bags. This is nice in that it doesn't require a feature deployed to the environment so I use either this or the pbs2010 feature that @Deepu Nair recommended.
Answer 4
To interact with the PropertyBags in code you simply refer to the object's Propeties member.
There are PropertyBags available for the SPFarm, SPWebApplication, SPSite, and SPWeb.
To set a value: site.Properties["PropertyName"] = "foo";
To read a value: string myProperty= site.Properties["PropertyName"].ToString();
Answer 5
#------------------------Code-------------------------------- 
$url= Read-Host 'Enter the site Name'
$site = New-Object Microsoft.SharePoint.SPSite($url)
$rootWeb = $site.RootWeb

Write-Host -foregroundcolor Green "The current Site"$rootWeb 

$value= Read-Host 'Enter the property name'
$Adminurl = "propertyName"

$rootWeb.AllowUnsafeUpdates = $true;
$Currentvalue = $rootWeb.Properties[$Adminurl]
Write-Host -foregroundcolor Green "The current value of the property bag is "$Currentvalue

if (!$rootWeb.Properties.ContainsKey($Adminurl))
{ 
         $rootWeb.Properties.Add($Adminurl, $value);
}
else
{
         $rootWeb.Properties[$Adminurl] = $value;
}                        

       $rootWeb.Properties.Update();
       $rootWeb.Update();

       $rootWeb.AllowUnsafeUpdates = $false;

$UpdatedValue =  $rootWeb.Properties[$Adminurl]
Write-Host -foregroundcolor Green "Value of the property bag is updated with " $UpdatedValue


if ($rootWeb -ne $null)
{
    $rootWeb.Dispose()

}
If ($site -ne $null)
{
    $site.Dispose();
}

Write-Host -foregroundcolor Green "Script has finished executing "

Using c#:

SPSite site = SPContext.Current.Site;
{
    using (SPWeb spWebRoot = site.RootWeb)
    {

        // unsafe updates are required to be able to write to the property bag
        string key = "administrationurl";
        string myValue = "nikh";
        spWebRoot.AllowUnsafeUpdates = true;

        // you must check to see if the collection has a value in the assigned key already

        if (!spWebRoot.AllProperties.ContainsKey(key))

        spWebRoot.Properties.Add(key, myValue);

        else

        spWebRoot.AllProperties[key] = myValue;



        // update the properties
        spWebRoot.Update();

        spWebRoot.Properties.Update();
        spWebRoot.AllowUnsafeUpdates = false;
    }
}

No comments:

Post a Comment