SPC050236: Do not call SPItemEventProperties.OpenWeb()

Do not call SPItemEventProperties.OpenWeb(). Use the newly introduced SPItemEventProperties.Web property instead for better performance and to avoid the need to call Dispose().

TypeName: DoNotCallSPItemEventPropertiesOpenWeb
CheckId: SPC050236
Severity: CriticalWarning
Type: AssemblyFileReference
Resolution

Replace calls to SPItemEventProperties.OpenWeb() with SPItemEventProperties.Web property for better performance and to avoid the need to call Dispose().

Bad Practice:

public override void ItemDeleting(SPItemEventProperties properties)
{
  SPWeb web = properties.OpenWeb();
  // Operate on the SPWeb object which needs later Dispose.
  ...     
}
Good Practice:
public override void ItemDeleting(SPItemEventProperties properties)
{
  SPWeb web = properties.Web;
  // Operate on the SPWeb object with better performance and without need for Dispose.
  ...     
}

Links

comments powered by Disqus