Skip to main content
2 of 3
edited tags
Eric Alexander
  • 43.3k
  • 10
  • 54
  • 93

How to detect if my code is running inside SPSecurity.RunWithElevatedPrivileges

Is there a way to detect if my code is running inside SPSecurity.RunWithElevatedPrivileges?

class Foo
{
  public static void Bar()
  {
     if (CODE_RUNS_ELEVATED)
     {
       Debug.WriteLine("You are elevated");
     }
     else
     {
       Debug.WriteLine("You are not elevated");
     }
  }
}

SPSecurity.RunWithElevatedPrivileges(() => Foo.Bar()); // Prints:  You are elevated
Foo.Bar(); // Prints: You are not elevated

My problem is how to resolve CODE_RUNS_ELEVATED.

Matthias
  • 2.1k
  • 13
  • 22