我创建了一个Eventhandler(SPEventReceiverType.ItemDeleting),当用户从特定的SPList中删除SPListItem时,会调用它。对于每个项目都是一个网站,我想要简单地向用户询问是/否 - MessageBox是否应该删除该网站... 并且存在问题,经过一段时间在谷歌上,我意识到我找不到一些有用的提示如何显示MessageBox,如果点击是,如何继续做其他代码。
无论如何,通常我会这样做,在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
}
有时候,SharePoint让我疯狂。 真诚地,Markus Schwalbe
P.S。如果你愿意,你可以尝试解决我的另一个问题:)
答案 0 :(得分:0)
您可以这样做:
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
将包含一个自定义MessageBox webPart,它将根据响应删除项目。它将从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);
}
}
答案 1 :(得分:0)
你想要做的是/否确认应该在处理之前它甚至到达事件接收者。例如,如果我从计划作业,PowerShell或内容部署导入/导出中删除项目,则无法捕获用户输入。更糟糕的情况是,进程阻止永远等待永远不会来的用户输入。
例如,应该使用SPEventReceiverType.ItemDeleting事件来根据站点中的信息验证删除(例如,停止删除查找字段中的引用数据),如果失败,则使用属性取消它。 / p>
properties.Cancel = true;
properties.ErrorMessage = "Can not delete";