add object to farm properties from an event receiver

Can you add an object to farm properties from an event receiver on a list in a root site of a site collection? I have tried but even if im logged in as a farm admin it always errors out with a security exception.

public override void ItemAdded(SPItemEventProperties properties){
            base.ItemAdded(properties);

            SPFarm Farm = SPFarm.Local;
            if (Farm == null) return;

            if (Farm.Properties != null)
            {
                if (!Farm.Properties.ContainsKey(Common.FarmPropertyKey)){
                    Farm.Properties.Add(
                        Common.FarmPropertyKey, 
                        "Test Value " + DateTime.Now.ToString());
                }
                else{
                    Farm.Properties[Common.FarmPropertyKey] = 
                        "Test Value " + DateTime.Now.ToString();
                }

                Farm.Update();
            }
        }


  • Edited by ricobano 20 hours 42 minutes ago
April 1st, 2014 10:36am

try using RunWithElevatedPrivileges block.

Arguably, I'm not a huge fan of using a FARM level property bag, but I'm assuming you're doing something for license keys... which is an understandable use.

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2014 11:45am

Hi ricobano,

Using SPSecurity.RunWithElevatedPrivileges seems also access denied in the event receiver due to security issue.

I would suggest you the following article to write value to property bag on the web instead of the farm property:

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/05/12/how-to-write-a-value-into-the-property-bag.aspx

Thanks,

April 2nd, 2014 4:22am

runwithelevatedpriviles means the code will run under the service account for the web application.  typically this will not be enougth to add entries to the farm properties bag
Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2014 5:31am

it needs to be at the farm level as the object stored needs to be available in every web application across the farm
April 2nd, 2014 5:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics