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 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)
                            {
share|improve this question
    
I am already creating the SPSite inside run with elegated privilies, thats the weird thing –  Esteban V May 24 '13 at 9:50

3 Answers 3

up vote 2 down vote accepted

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();
share|improve this answer
    
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. –  Esteban V May 25 '13 at 15:13

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.

share|improve this answer

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

share|improve this answer

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.