I've created an Eventhandler (SPEventReceiverType.ItemDeleting) which is called when a user is deleting an SPListItem from a specific SPList. For every item theres an website and I want to simple ask the user with a yes/no-MessageBox if it should delete the site as well... And there is the problem, after quite some time on google, I realized that I couldn't find some usuful hints how to show a MessageBox, and if clicked Yes, how to continue doing some other code.
Anyway, the usual way I would do this, doesn't works in a SharePoint 2010 EventHandler:
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
}
Sometimes, SharePoint is driving me nuts. sincerely, Markus Schwalbe
P.S. If you like, you can try to solve my other question :)
You can do something like this:
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
will contain a custom MessageBox webPart which will either delete the item or not depending on the response. It will read the ID from Url query string. Here is snippet:
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);
}
}
The YES/NO confirmation you are trying to do, should be handled before it even gets to the event receiver. If for example, i was deleting the item from a scheduled job, or from powershell, or from a content deployment import/export, there is no way i can capture user input. Worse case the process blocks forever waiting for user input that never comes.
The SPEventReceiverType.ItemDeleting event should be used, for example, to validate a delete based on information in the site (eg stop deleting referenced data in a lookup field) and if it fails, then use the properties to cancel it.
properties.Cancel = true;
properties.ErrorMessage = "Can not delete";
精彩评论