gpt4 book ai didi

c# - 在事件接收器中获取 SharePoint ListItem 的 After 和 Before 属性

转载 作者:太空宇宙 更新时间:2023-11-03 20:13:02 25 4
gpt4 key购买 nike

因此,我正尝试在我的 SharePoint 2010 列表中完成此类功能:

我的列表中有一个类型选择字段,它有 7 个值,我希望用户不能将该字段的值从值 2、3、4、5、6、7 更改为值 1。

我已经为该列表编写了一个事件接收器,这是我的代码:

public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);

string beforeStatus = properties.BeforeProperties["Status"].ToString();
string afterStatus = properties.AfterProperties["Status"].ToString();

if (beforeStatus != "1stValue" && afterStatus == "1stValue")
{
properties.Cancel = true;
properties.ErrorMessage = "This isn't allowed.";
}
}

我试过同时使用 ItemUpdatedItemUpdating 事件接收器,当我调试时我看到事件接收器 get 被调用了,但是 在这两种情况下,beforeStatusafterStatus 都从项目中获取 null

那么,如何正确获取item字段更新前后的值呢?提前致谢!

注意:字段的内部名称和显示名称都是Status

最佳答案

使用 ItemUpdating 事件,然后 afterproperties 包含更改的值,ListItem 包含字段的原始值。

Here您可以找到每个事件中可用的属性的信息。

如何编辑列表项也很重要。如果通过 SharePoint 默认编辑表单,所有列都存在于 afterproperties 集合中,但如果您从自定义代码(例如 webpart、事件接收)编辑项目,则只有更新的列存在于该集合中。

编辑:对于好看的错误,您可以将用户重定向到自定义错误页面(您必须创建)

properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = "/_layouts/MySolution/CustomErrorPage.aspx?Error=" + errorMessage;

关于c# - 在事件接收器中获取 SharePoint ListItem 的 After 和 Before 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com