Sharepoint 2010 事件接收器项目在另一个列表中添加和更新 [英] Sharepoint 2010 Event Receiver Item Adding and Updating in another List
问题描述
我有两个列表,我想在第一个列表中添加新项目的同时更新第二个列表.我正在使用以下代码,但没有任何反应.
i have two list, i wanted to update the second list while adding a new item in first one. i am using the following code but nothing happens.
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
string MenuList = properties.ListTitle;
if (MenuList == "Menu")
{
string mySiteUrl = "http://rizwan-pc";
using (SPSite mysiteCollection = new SPSite(mySiteUrl))
{
using (SPWeb mySite = mysiteCollection.RootWeb)
{
string title = properties.ListItem["Title"].ToString();
SPList Menu_lookUp = mySite.Lists["Menu LookUp"];
SPListItem newListItem = Menu_lookUp.Items.Add();
newListItem["Applicable"] = title;
newListItem.Update();
}
}
}
}
我想复制我当前添加到第一个列表中的第二个列表中的新项目.任何帮助都会很棒,提前致谢.
i wanted to copy the new item in the second list which i currently added in the first list. any help would be great, Thanks in advance.
推荐答案
如果你想要列表项的标题,你可以试试这个:
if you want to title of the listitem than you can try this :
在获取站点或网站的代码中使用属性的好习惯:
its good habit to use properties in your code for get site or web :
using(SPWeb objweb = properties.openweb())
{
string title = convert.ToString(properties.AfterProperties["Title"]);
objweb.AllowUnsafeUpdates = true;
SPList Menu_lookUp = mySite.Lists["Menu LookUp"];
SPListItem newListItem = Menu_lookUp.Items.Add();
newListItem["Applicable"] = title;
newListItem.Update();
objweb.AllowUnsafeUpdates = false;
}
一件事是你应该使用 item added
而不是 itemadding
.很容易通过当前id获取listitem.
one things is that you should use itemadded
instead of itemadding
. its easy to get listitem by current id.
SPList objlist = objweb.Lists[properties.ListTitle];
SPListItem liCaseID = objlist.Items.GetItemById(properties.ListItemId);
//your excute your code.
这篇关于Sharepoint 2010 事件接收器项目在另一个列表中添加和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!