5

Has any one worked with SharePoint Property Bags ? I found few helpful articles but that helped me only partially. I want to write a code that writes and retrieves information from Property Bag. I have the code with me but confused where to write it and what type of item should I select.

Thanks.

1

3 Answers 3

7

A good way to visualize this and to learn more about the structure of the property bags is to use SharePoint Manager and just browse the DOM. http://spm.codeplex.com/

For example to access the property bag of the root folder in a list you can use this code:

string someProperty = "Some data to store";
MyList.RootFolder.Properties["YourProperty"] = someProperty;

Then to read the property use something like this:

string someProperty = MyList.RootFolder.Properties["YourProperty"] as string;
1
  • 1
    I'm afraid you've forgotten to update the SPFolder object in your sample code, in order to save changes to database Jan 13, 2012 at 16:27
2

There is a codeplex project which could give you an overview of how property bags can be used in SharePoint 2010 or 2007. Check this out - http://pbs2010.codeplex.com/

1
  • After searching and working with Property Bags, things got cleared. You can update Property Bags using any SharePoint Object Model Code - Event Receiver, Visual Web Part, etc...
    – Zakir HC
    Jan 24, 2012 at 14:15
0
#------------------------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;
    }
}

Your Answer

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

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