/// <summary> /// SharePoint ClientContext に対して処理を試行します。 /// </summary> /// <param name="tryAction">試行するメソッド</param> /// <param name="catchAction">例外発生時に実行するメソッド</param> /// <param name="finallyAction">最終的に実行するメソッド</param> /// <remarks>例外発生時と最終的に実行する処理を指定できます。</remarks> public void TryExecute(Action <SP.ClientContext> tryAction, Action <SP.ClientContext> catchAction = null, Action <SP.ClientContext> finallyAction = null) { if (tryAction == null) { throw new ArgumentNullException(nameof(tryAction)); } this.ReferToContext(cn => { var scope = new SP.ExceptionHandlingScope(cn); using (scope.StartScope()) { using (scope.StartTry()) { // Try tryAction?.Invoke(cn); } using (scope.StartCatch()) { // Catch catchAction?.Invoke(cn); } using (scope.StartFinally()) { // Finally finallyAction?.Invoke(cn); } } cn.ExecuteQuery(); // 例外判定 if (!scope.ErrorMessage.IsEmpty()) { this.OnThrowSharePointException(scope); return; } this.OnSuccess(); }); }
/// <summary> /// SharePoint 例外発生時に呼び出されます。 /// </summary> /// <param name="scope">ExceptionHandlingScope</param> protected virtual void OnThrowSharePointException(SP.ExceptionHandlingScope scope) { if (this.ThrowSharePointException == null) { return; } var e = new ThrowSharePointExceptionEventArgs( scope.ErrorMessage , scope.HasException , scope.Processed , scope.ServerErrorCode , scope.ServerErrorDetails , scope.ServerErrorTypeName , scope.ServerErrorValue , scope.ServerStackTrace ); this.ThrowSharePointException(this, e); }