0

I have an application page which connects to a sql database based on settings on a list hosted in central admin.

For some reason, I cant debug the web.GetSafeListByName, I attach the debugger, put a breakpoint, the breakpoint is red which means the symbols are loaded, but the debugget doesnt get there.

Then It shows Access Denied on the page, and in the Exception it shows

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

public static String GetDMSApplicationSetting(String key)
        {
            String value = String.Empty;

            try
            {
                String caUrl = GetCentralAdminUrl();
                if (!String.IsNullOrEmpty(caUrl))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                    using (SPSite site = new SPSite(caUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList DMSConfigList = web.GetSafeListByName(Constants.Lists.DmsApplicationSettingsUrl);
                            if (DMSConfigList!=null)
                            {
1
  • I am already creating the SPSite inside run with elegated privilies, thats the weird thing May 24, 2013 at 9:50

3 Answers 3

2

I agree with James, your code is being run as application pool account and it shouldn't have access to manipulate SharePoint configuration database.

You can always use property bags at Farm level to store the settings:

Add property in SPFarm level

            SPFarm myFarm = SPFarm.Local;
            myFarm.Properties.Add("SPFarmKey", "SPFarmValue");
            myFarm.Update();

Get the SPFarm property

        SPFarm myFarm = SPFarm.Local;
        if (myFarm.Properties != null && myFarm.Properties.Count > 0)
        {
            if (myFarm.Properties.ContainsKey("SPFarmKey"))
            {
                Console.WriteLine(myWebApplication.Properties["SPFarmKey"]);
            }
        }

Update the property in SPFarm level

        SPFarm myFarm = SPFarm.Local;
        myFarm.Properties["SPFarmKey"] = "NewSPFarmValue";
        myFarm.Update();
1
  • indeed I already know about farm property bags, but the people in charge of the project decided to put a list in central admin, when we were not able to fix the access denied problem, I refactored the existing code and created a farm feature to create the properties. And by the way its even easier than accessing lists. May 25, 2013 at 15:13
1

RunWithElevated only runs the code as the Application Pool Identity account. In a standard, best practice environment, this will not have access to Central Admin (for good reason).

If you want to store settings that you can get to globally in a farm, try using the SecureStoreService API. It's not just for passwords.

0

Try to put your Application page On ADMIN folder instead of LAYOUTS, take a look at this article

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.