Expand Minimize

Do not set SPSite.CatchAccessDeniedException property

Property SPSite.CatchAccessDeniedException is reserved for internal use. Use the SPSecurity.CatchAccessDeniedException property instead to specify whether SharePoint Foundation traps and handles "Access Denied" exceptions.

CheckId SPC010212
TypeName DoNotCallSPSiteCatchAccessDeniedException
Severity CriticalWarning
Type Assembly

Use SPSecurity.CatchAccessDeniedException instead of using SPSite.CatchAccessDeniedException. Mircosoft warns to use members which are reserved for internal use only because your code could break in the next hotfix or service pack. See sample below:
Bad Practice

using (SPSite spSite = new SPSite("http://url/"))
{
  bool originalCatchValue = spSite.CatchAccessDeniedException;  
  spSite.CatchAccessDeniedException = false;
  try
  {
      // Perform the task here
  }
  finally
  {
      spSite.CatchAccessDeniedException = originalCatchValue;
  }
}

Good Practice
using (SPSite spSite = new SPSite("http://url/"))
{
  bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
  SPSecurity.CatchAccessDeniedException = false;
  try
  {
      // Perform the task here
  }
  finally
  {
      SPSecurity.CatchAccessDeniedException = originalCatchValue;
  }
}

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("SPCAF.Rules.CorrectnessGroup", "SPC010212:DoNotCallSPSiteCatchAccessDeniedException", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.