16

What is property bag in SharePoint 2010?

How do I retrieve values from a property bag and get/set the values using powershell?

1

5 Answers 5

21
#------------------------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;
    }
}
1
  • When using Powershell : make sure to update the properties & allproperties()
    – Nikhil J
    Commented Oct 17, 2011 at 12:20
6

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

6
  • "the content database may get corrupted" - really?
    – Ryan
    Commented Oct 14, 2011 at 8:45
  • The content database may get corrupted if you try to store non-serializable objects in property bags. Commented Oct 14, 2011 at 9:51
  • 1
    "Corrupted" is a very strong word you're implying something that simply will not happen. The property bag is just a key/value store and you can put whatever you want in whatever format you want. If your object is not serializable then you would have to provide a custom serialization scheme anyway.
    – Ryan
    Commented Oct 17, 2011 at 8:32
  • 3
    I think we should not put whatever we want in Property bags. I will recommend to stick to the guidelines from msdn here msdn.microsoft.com/en-us/library/ff649798.aspx. I agree that content DB will not corrupt so easily by a developer mistake. However, it may not be true for all kind of mistakes. So again it is better to follow "Patterns & Practices' recommended at msdn here msdn.microsoft.com/en-us/library/ff770300.aspx. As per the guidance "developers risk corrupting the configuration database or the content database if they attempt to persist non-serializable types.." Commented Oct 17, 2011 at 9:28
  • 1
    +1 @Amit. I stand corrected, though I don't know what manner of WTF is going on for Microsoft to flag this to be a possible issue!
    – Ryan
    Commented Oct 17, 2011 at 13:50
3

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

2

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

0

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.

2
  • I think it is only useful if you know at design-time what you want to store in property bags. This is not the case most of the time when you implement complex business logic Commented Oct 14, 2011 at 14:30
  • Sorry, I misunderstood your question. I will post a different answer. Commented Oct 14, 2011 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.