1

I am using property bag to store config value using below code.

SPFarm farmObject = SPFarm.Local;
farmObject .Properties.Add("PropertyKey", "PropertyValue");
farmObject .Update();

This code is working fine. But Now if i want to update and remove property than it is not working.

SPFarm farmObject = SPFarm.Local;
 if (farmObject.Properties != null && farmObject.Properties.Count > 0)
 {
 if (farmObject.Properties.ContainsKey("PropertyKey"))
 {
  farmObject.Properties["PropertyKey"] = "New Property Value";
         farmObject.Update();
 }
 }

Same case for remove also. In farmObject.Update line nothing happens. No error, no message and nothing is working.I have debug but after this update line nothing is happen and debug is not work further. Once after adding farmObject.Update() is not working for update and also for remove also.

I have custom application page in central admin. And on click of button click I have write this code.

1

2 Answers 2

2

Really, it seems to me like your code sample should work.

Check out storing custom objects in sharepoint farm property bags:

Modify the property value farm level
------------------------------------
SPFarm farmObject = SPFarm.Local;
if (farmObject.Properties != null && farmObject.Properties.Count > 0)
{
    if (farmObject.Properties.ContainsKey("PropertyKey"))
    {
        farmObject.Properties["PropertyKey"] = "New Property Value";
        farmObject.Update();
    }
}

It's the exact same code as what you have.

It's also the exact same code found here and here.

4
  • If calling Update() on the Properties object pushes the changes to the database, is there even a need to call Update() on the Farm object? Jan 28, 2014 at 16:27
  • Honestly, I'm not sure. You could test it and figure it out. I've found code samples online that don't use the properties update method at all (which seems like they may be wrong?), and some that use both updates. Jan 28, 2014 at 17:34
  • 2
    Interesting. After a quick test in Powershell, I got an error because System.Collections.Hashtable doesn't contain a method named 'Update'! So you can't actually do farmObject.Properties.Update(). (I also verified that there is no Update method available on SPFarm.Local.Properties in VS.) I did however, manage to get a new value saved using just farmObject.Update(), so I'm not sure where the OP's problem is really coming from. And maybe those code samples you found that don't use the properties update method are actually right. Jan 28, 2014 at 21:02
  • Seems you're right. msdn.microsoft.com/en-us/library/… Jan 28, 2014 at 21:20
1

Remove farm key using this code,

farmObject.AllProperties.Remove("PropertyKey"); 
farmObject.Properties["PropertyKey"] = null; 
farmObject.Update(); 
farmObject.Properties.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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