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

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.

share|improve this question
    
add comment

3 Answers

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;
share|improve this answer
1  
I'm afraid you've forgotten to update the SPFolder object in your sample code, in order to save changes to database –  Andrey Markeev Jan 13 '12 at 16:27
add comment

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/

share|improve this answer
    
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 '12 at 14:15
add comment
#------------------------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
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.