Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What is property bag in SharePoint 2010?

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

share|improve this question
    
Here is the post explaining to get and set property bag values in SharePoint 2013 apps using CSOM sureshpydi.blogspot.in/2013/05/… –  suresh May 29 '13 at 4:45
add comment

5 Answers 5

up vote 8 down vote accepted
#------------------------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;
    }
}
share|improve this answer
    
When using Powershell : make sure to update the properties & allproperties() –  Nikhil Oct 17 '11 at 12:20
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
"the content database may get corrupted" - really? –  Ryan Oct 14 '11 at 8:45
    
The content database may get corrupted if you try to store non-serializable objects in property bags. –  Amit Kumawat Oct 14 '11 at 9:51
    
"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 Oct 17 '11 at 8:32
2  
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.." –  Amit Kumawat Oct 17 '11 at 9:28
    
Very interesting about the non-serializable objects. –  Kit Menke Oct 17 '11 at 13:17
show 1 more comment

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

share|improve this answer
add comment

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.

share|improve this answer
    
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 –  Amit Kumawat Oct 14 '11 at 14:30
    
Sorry, I misunderstood your question. I will post a different answer. –  Mike Oryszak Oct 14 '11 at 14:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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