最近写事件接收器,发现中文字段如果直接用properties.AfterProperties[“申请人"]这样获取的值为null,无法得到值。后拉忽然发现用英文字段可以得到值。难道中文字段需要编码?经过测试果真如此。
代码部分如下:
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
//得到站点
SPWeb web = properties.OpenWeb();
//如果web不为空
if (web != null)
{
//得到列表
SPList list = web.Lists[properties.ListId];
//如果列表不为空
if (list != null)
{
//得到字段
string fieldName = list.Fields.GetField(WorkLeaveConfig.WorkLeave_Title).InternalName;
//得到值
string WorkTitle = properties.AfterProperties[fieldName] == null ? string.Empty : properties.AfterProperties[fieldName].ToString();
//得到值
if (WorkTitle.IndexOf("测试") >= 0)
{
properties.ErrorMessage = "无法请假!";
properties.Cancel = true;
}
}
}
}
效果图如下: