Sharepoint 2010 事件接收器在引用列表之前不受影响
我有一个Sharepoint 2010列表。我已经编写了事件接收器代码来更新item added事件中的字段值。我得到了正确的结果,但在我刷新列表之前,它不会更新字段值。为什么? 代码: 添加了公共覆盖无效项(SPItemEventProperties属性) { 基本。添加的项目(属性); SPWeb=properties.OpenWeb(); SPList list=properties.list; int最高值=0; SPQuery query=新建SPQuery(); query.query=@” 1"; SPListItemCollection itemcollection=list.GetItems(查询); 如果(itemcollection.Count>0) { SPListItem=itemcollection[0]; 最高值=转换为32(项目[“下一个”]); } SPListItem currItem=properties.ListItem; currItem[“NextNo”]=最高值+1; currItem.Update(); }Sharepoint 2010 事件接收器在引用列表之前不受影响,sharepoint-2010,Sharepoint 2010,我有一个Sharepoint 2010列表。我已经编写了事件接收器代码来更新item added事件中的字段值。我得到了正确的结果,但在我刷新列表之前,它不会更新字段值。为什么? 代码: 添加了公共覆盖无效项(SPItemEventProperties属性) { 基本。添加的项目(属性); SPWeb=properties.OpenWeb(); SPList list=properties.list; int最高值=0; SPQuery query=新建SPQuery(); query.quer
您需要使用
项目添加
事件接收器,因为它会在更新列表之前触发,并显示您的更改。itemsadded
事件在更新列表后激发,因此需要刷新
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPWeb web = properties.OpenWeb();
SPList list = properties.List;
int highestValue = 0;
SPQuery query = new SPQuery();
query.Query = @"<OrderBy>
<FieldRef Name='NextNo' Ascending='FALSE' />
</OrderBy><RowLimit>1</RowLimit>";
SPListItemCollection itemcollection = list.GetItems(query);
if (itemcollection.Count > 0)
{
SPListItem item = itemcollection[0];
highestValue = Convert.ToInt32(item["NextNo"]);
}
SPListItem currItem = properties.ListItem;
currItem["NextNo"] = highestValue + 1;
currItem.Update();
}