只有当 ListItem 有附件时,我才对 itemAdded EventReceiver 有奇怪的问题。所谓的保存冲突错误当然会停止 EventReceiver。如果新的 ListItem 没有附件,那么一切正常。
请看一下我的代码。也许你会找到解决它的方法。
我已经尝试过 allowunsafeupdates、eventfiringEnable/Disable、item.SystemUpdate() 或 .UpdateOverwriteVersion() 等。
这个 ER 的目标只是从其他列表中收集一些数据,计算附件等。正常操作最适合 itemAdded。
此外,我将与列表工作流相关联(从 SharePoint Designer - 设置为手动) - 分享您关于如何执行此操作的想法。
具有最新更新的 Sharepoint Server 2019(6 月 21 日)。
LogOps 方法用于保存一些日志 - 它不是我的 - 只是在网络中找到的。
public override void ItemAdded(SPItemEventProperties properties)
{
SPWeb web = properties.Web;
if (properties.ListTitle.Contains("Request"))
{
SPListItem itemAdded = properties.ListItem;
SPList listRequesters = web.Lists["Requesters"];
SPList listOrganisations = web.Lists["Organisations"];
string username = new SPFieldUserValue(web, itemAdded["Author"].ToString()).User.Name;
string queryString = @"<Where><Eq><FieldRef Name='Requester' /><Value Type='User'>" + username + @"</Value></Eq></Where>";
SPQuery query = new SPQuery
{
Query = queryString
};
SPListItemCollection itemresRequesters = listRequesters.GetItems(query);
string sapNumber = "";
if (itemresRequesters.Count == 1)
{
foreach (SPListItem itemresult in itemresRequesters)
{
sapNumber = itemresult["SAPNumber"].ToString();
itemAdded["Manager"] = itemresult["ManagerL1"];
itemAdded["SAPNumber"] = itemresult["SAPNumber"];
itemAdded.Update();
}
}
else
{
if (itemresRequesters == null)
{
LogOps("ERItemAdded", "Missing requesters on Requesters list");
}
else
{
LogOps("ERItemAdded", "Requesters - multiple users of the same username :" + itemresRequesters.Count);
}
}
string queryStringOrg = @"<Where><Eq><FieldRef Name='SAPNumber' /><Value Type='Text'>" + sapNumber + @"</Value></Eq></Where>";
SPQuery queryOrg = new SPQuery
{
Query = queryStringOrg
};
SPListItemCollection itemresOrganisations = listOrganisations.GetItems(queryOrg);
if (itemresOrganisations.Count == 1)
{
foreach (SPListItem itemresult in itemresOrganisations)
{
itemAdded["ManagerL2"] = itemresult["Manager L2"];
itemAdded["ManagerL3"] = itemresult["Manager L3"];
itemAdded["Boss"] = itemresult["Boss"];
itemAdded.Update();
LogOps("ERItemAdded", "Higher Managers updated");
}
}
else
{
if (itemresOrganisations == null)
{
LogOps("ERItemAdded", "Organisations - no such Organisation");
}
else
{
LogOps("ERItemAdded", "Organisations - too many Organisations :" + itemresOrganisations.Count);
}
}
//Request Number
DateTime data = (DateTime)itemAdded["Created"];
string numerWniosku = sapNumber +"-" + data.ToString("yyyyMMdd") + "-" + itemAdded["ID"];
itemAdded["Request Number"] = numerWniosku;
LogOps("ERItemAdded", "Request number : " + itemAdded["Request Number"].ToString());
//Request status information
itemAdded["Request Status"] = "Launched";
//Count Attachements
int att = itemAdded.Attachments.Count;
itemAdded["Att"] = att.ToString();
itemAdded.Update();
LogOps("ERItemAdded", "Attachments counted : " + itemAdded.Attachments.Count.ToString());
LogOps("#############", "End of ER");
}
else
{
LogOps("ERItemAdded", "Improper list");
}
}
private void LogOps(string strEvent, string message)
{
FileIOPermission fp = new FileIOPermission(PermissionState.Unrestricted);
fp.AddPathList(FileIOPermissionAccess.AllAccess, @"d:\Logs");
StreamWriter sw = File.AppendText(@"D:\Logs\ER_Requests_LOG.txt");
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.ToString() + " | " + strEvent +" | " + message + "\n");
sw.WriteLine(sb.ToString());
sw.Close();
}
}
'''