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

I am trying to add a new property to the SPFarm property bag but am getting an access denied security exception. As you can see in the code below it is begin ran with elevated privileges, so I don't really know what to try next. Any advice? Is there a better way to do what I am trying to do (store a value any instance of that webpage can grab and read/change)? I read this: http://msdn.microsoft.com/en-us/library/ee855123(v=office.12).aspx and it looks like the property bag is the best route for me, just need to figure out how to add to it...

Here is the code:

SPFarm.Local.Properties.Add(LastEmployeeIDKey, 4000);
SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(SPFarm.Local.Update));

Edit: Tried moving down to the web application level but still get the same error. Here is the code I am using now:

SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(
   delegate()
   {
      SPWebApplication.Lookup(SiteURI).Properties.Add(LastEmployeeIDKey, 4000);
      SPWebApplication.Lookup(SiteURI).Update();
    }));

Thanks for any help.

Edit: Finally found a way. For those interested it is explained here: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/05/12/how-to-write-a-value-into-the-property-bag.aspx

share|improve this question
add comment

1 Answer

It is normally not a great idea to use the property bag at the farm level, I've heard of issues where problems there led to corrupt farm configuration, though I cannot remember the specific scenario. I tend to use the web application instead.

With regards to the issue, when you run with elevated privileges it will use the application pool identify. The application pool identity has full access to the web application, but not to the farm.

share|improve this answer
    
Tried at application level, still won't work. Updated code to show what I am doing now. Anymore ideas? –  Josh Apr 5 '13 at 12:59
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.