2011-01-10 5 views
0

私は、ユーザーが特定のSPListからSPListItemを削除するときに呼び出されるイベントハンドラ(SPEventReceiverType.ItemDeleting)を作成しました。すべての項目については、ウェブサイトを扱うためにサイトを削除する必要がある場合はYes/No-MessageBoxで簡単にユーザーに聞きたいのですが... Googleでかなりの時間が経過した後、 MessageBoxを表示する方法や、他のコードをやり続ける方法をクリックした場合のヒントを見つけることができませんでした。SharePoint 2010イベントハンドラでシンプルなYES/NO MessageBoxを使用するにはどうすればよいですか?

if (MessageBox.Show("Do you want to delete the site as well?", "Delete?", 
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
{ 
    // Continue Here if "Yes" has been clicked 
} 

時には、SharePointは私にナットを運転している:

とにかく、私はこれを行うだろう通常の方法では、SharePoint 2010のイベントハンドラでの作品にはありません。誠実に 、Markus Schwalbe

P.S.

public override void ItemDeleting(SPItemEventProperties properties) 
     { 

      string updateID = Convert.ToString(properties.ListItem["ID"], CultureInfo.InvariantCulture); 
      string itemUID = tmpItem.UniqueId.ToString(); 
      string urlProcessing = string.Format(CultureInfo.InvariantCulture, Constants.StringRedirect, web.Url, Constants.PersonRecord, "Processing.aspx", itemUID, updateID, modifyTime); 
      properties.Status = SPEventReceiverStatus.CancelNoError; 
      SPUtility.Redirect(urlProcessing, SPRedirectFlags.Static, this.currentContext); 

    } 

Processing.aspxに応じた項目を削除したりしませんいずれかのカスタムメッセージボックスWebパーツが含まれます:あなたが好きなら、あなたはこのような何かを行うことができます、私の他の質問:)

答えて

0

を解決しようとすることができます応答。 Urlクエリ文字列からIDを読み取ります。ここでは抜粋です。

using System; 
using System.Collections.Generic; 
using System.Diagnostics.CodeAnalysis; 
using System.Globalization; 
using System.Runtime.InteropServices; 
using System.Security.Permissions; 
using System.Text; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 


/// <summary> 
/// This class contains operations to check if a username is duplicated or not 
/// </summary> 
[Guid("ddeaa4a4-6d63-4fc8-9a17-8874db582388")] 
[SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] 
[SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)] 
[CLSCompliant(false)] 
public class ConfirmWebpart : Microsoft.SharePoint.WebPartPages.WebPart 
{ 
    /// <summary> 
    /// Duplicated username 
    /// </summary> 
    private string userName; 

    /// <summary> 
    /// UNID of a item 
    /// </summary> 
    private string itemID; 

    /// <summary> 
    /// ID of updating item 
    /// </summary> 
    private string updateID; 

    /// <summary> 
    /// Last edit time 
    /// </summary> 
    private string lastEdit; 

    /// <summary> 
    /// To check error in the application. 
    /// </summary> 
    private bool error; 

    /// <summary> 
    /// Current context 
    /// </summary> 
    private HttpContext currentContext; 

    /// <summary> 
    /// Initializes a new instance of the ConfirmWebpart class. 
    /// </summary> 
    public ConfirmWebpart() 
    { 
     this.currentContext = HttpContext.Current; 
     this.ExportMode = WebPartExportMode.All; 
    } 

    /// <summary> 
    /// Ensures that the CreateChildControls() is called before events. 
    /// Use CreateChildControls() to create your controls. 
    /// </summary> 
    /// <param name="e">e parameter</param> 
    [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", Justification = "This is to catch all possible exceptions and to prevent exceptions to bubble up on screen.")] 
    protected override void OnLoad(EventArgs e) 
    { 
     if (!this.error) 
     { 
      try 
      { 
       base.OnLoad(e); 
      this.EnsureChildControls(); 
      } 
      catch (Exception ex) 
      { 
       this.HandleException(ex); 
       throw; 
      } 
     } 
    } 

    /// <summary> 
    /// Create all your controls here for rendering. 
    /// Try to avoid using the RenderWebPart() method. 
    /// </summary> 
    protected override void CreateChildControls() 
    { 
     if (!this.error) 
     { 
      try 
      { 
       base.CreateChildControls(); 
       this.GetQueryStringValues(); 
       this.RenderContents(); 
      } 
      catch (Exception ex) 
      { 
       this.HandleException(ex); 
       throw; 
      } 
     } 
    } 

    /// <summary> 
    /// Clear all child controls and add an error message for display. 
    /// </summary> 
    /// <param name="ex">ex parameter</param> 
    protected void HandleException(Exception ex) 
    { 
     this.error = true; 
     this.Controls.Clear(); 
     this.Controls.Add(new LiteralControl(ex.Message)); 
    } 

    /// <summary> 
    /// Get all querystring value and assign to private members 
    /// </summary> 
    protected void GetQueryStringValues() 
    { 
     this.itemID = Page.Request.QueryString[Constants.ItemD]; 
     this.updateID = Page.Request.QueryString[Constants.Update]; 
     this.lastEdit = Page.Request.QueryString[Constants.LastEdit]; 
    } 

    /// <summary> 
    /// Render UI form 
    /// </summary> 
    protected void RenderContents() 
    { 
     this.GetDuplicatedUserName(); 

     this.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\"><tr style=\"height:50px\"><td style=\"width:50px\"></td><td colspan=\"3\"></td></tr><tr style=\"font-size: medium;color: #3366FF;height:40px\"><td></td><td colspan=\"3\">")); 
     this.Controls.Add(new LiteralControl(string.Format(CultureInfo.CurrentCulture, Constants.ConfirmMessage, this.userName))); 
     this.Controls.Add(new LiteralControl("</tr><tr><td></td><td colspan=\"3\"></td></tr><tr><td style=\"color: #3366FF\"></td><td colspan=\"3\" style=\"color: #3366FF\">")); 
     this.Controls.Add(new LiteralControl(Constants.YesAction)); 
     this.Controls.Add(new LiteralControl("</tr><tr><td style=\"color: #3366FF\"></td><td colspan=\"3\" style=\"color: #3366FF\">")); 
     this.Controls.Add(new LiteralControl(Constants.NoAction)); 
     this.Controls.Add(new LiteralControl("</td></tr><tr><td></td><td colspan=\"3\" style=\"height:20px\"></td></tr><tr><td style=\"width:100px\"></td><td style=\"width:100px\"></td><td>")); 

     //// Add Yess button 
     Button btnYes = new Button(); 
     btnYes.Text = Constants.YesButtonLabel; 
     btnYes.Width = new Unit(70); 
     btnYes.Click += new EventHandler(this.BtnYesClick); 
     this.Controls.Add(btnYes); 

     this.Controls.Add(new LiteralControl("</td><td>")); 
     //// Add No button 
     Button btnNo = new Button(); 
     btnNo.Text = Constants.NoButtonLabel; 
     btnNo.Click += new EventHandler(this.BtnNoClick); 
     btnNo.Width = new Unit(70); 
     this.Controls.Add(btnNo); 
     this.Controls.Add(new LiteralControl("</td></tr></table>")); 
    } 



    /// <summary> 
    /// This method is used to handle the click event of Yes button 
    /// </summary> 
    /// <param name="sender">sender parameter</param> 
    /// <param name="e">argument parameter</param> 
    private void BtnYesClick(object sender, EventArgs e) 
    { 
     //Delete the item 
    } 

    /// <summary> 
    /// This method is used to handle the click event of No button 
    /// </summary> 
    /// <param name="sender">sender parameter</param> 
    /// <param name="e">argument parameter</param> 
    private void BtnNoClick(object sender, EventArgs e) 
    { 
     // Redirect to the default view of People list 
     string defaultView = string.Format(CultureInfo.InvariantCulture, Constants.WebRedirectUrl, SPContext.Current.Web.Url, Constants.ListName); 
     SPUtility.Redirect(defaultView, SPRedirectFlags.Trusted, this.currentContext); 
    } 

}

0

YES /あなたがやろうとしているNO確認は、それもイベントレシーバに到達前に扱われるべきではありません。たとえば、スケジュールされたジョブやPowerShell、またはコンテンツ配備のインポート/エクスポートからアイテムを削除していた場合、ユーザー入力を取得する方法はありません。プロセスがユーザーの入力が決して来ないのを永遠に待っているという事態が起こることは間違いありません。

SPEventReceiverType.ItemDeletingイベントは、サイト内の情報に基づいて削除を検証する(たとえば、参照フィールドで参照されるデータを削除しないなど)場合に使用し、失敗した場合はプロパティを使用して取り消します。

properties.Cancel = true; 
properties.ErrorMessage = "Can not delete"; 
関連する問題