SharePoint ItemUpdated事件接收器错误 [英] SharePoint ItemUpdated Event Receiver Error
问题描述
伙计们,这是我的要求.
每当用户在SP列表中创建/更新/删除记录时,我都必须更新sql表.
这是一个简单的列表,其中包含4列(其中一个是查阅列)
我已经创建了一个事件接收器:添加了项目,删除可以工作,但更新不起作用.
我想在有人编辑记录时删除并插入行(因为该记录具有多个查找值选择列).
下面是我的代码,只能从此插入而不是删除,并且我找不到它为何如此的具体原因.
任何帮助表示赞赏.
代码:
公共重写void ItemUpdated(SPItemEventProperties属性){
//base.ItemUpdated(properties);
SPWeb网站= properties.OpenWeb();
SPList列表= web.Lists ["Misc Doc Divisions"];
SPListItem项目;
mdlistitemID = properties.ListItem.UniqueId.ToString();
SPFieldLookupValueCollection lValues = properties.ListItem ["Associated_x0020_Division_x0020_"] as SPFieldLookupValueCollection;
SPFieldLookupValueCollection lValuesI = properties.ListItem ["Associated_x0020_Division_x0020_"] as SPFieldLookupValueCollection;
count = lValues.Count;
SPEventLog("Updated","Count:" + count +"lValues [0]" + lValues [0],属性);
foreach(lValues中的SPFieldLookupValue lvalue)
{
lookupListItemIDU = lvalue.LookupId;
lookupListItemValueU = lvalue.LookupValue;
item = list.GetItemById(lookupListItemIDU);
guidU = item.UniqueId;
试试
{
使用(SqlConnection connD =新的SqlConnection())
{
connD.ConnectionString =<连接字符串>" ;;
SqlCommand cmd =新的SqlCommand(从<表名>删除,其中MiscDocumentsListItemID = @mdLiID和MiscDocDivisionsListItemID = @mddLiID", connD);
cmd.Parameters.AddWithValue("@ mdLiID",mdlistitemID);
cmd.Parameters.AddWithValue("@ mddLiID",guidU.ToString());
connD.Open();
cmd.ExecuteNonQuery();
connD.Close();
}
}
catch(异常例外)
{
SPEventLog("MD2D Delete Error",例如消息,属性);
}
}
foreach(lValuesI中的SPFieldLookupValue lvalueI)
{
lookupListItemIDU = lvalueI.LookupId;
lookupListItemValueU = lvalueI.LookupValue;
item = list.GetItemById(lookupListItemIDU);
guidU = item.UniqueId;
试试
{
使用(SqlConnection connU =新的SqlConnection())
{
connU.ConnectionString =<连接字符串>" ;;
SqlCommand cmdU =新的SqlCommand("INSERT INTO<表名>([[MiscDocumentsListItemID],[MiscDocDivisionsListItemID])值(@mdLiID,@mddLiID)", connU);
cmdU.Parameters.AddWithValue("@ mdLiID",mdlistitemID);
cmdU.Parameters.AddWithValue("@ mddLiID",guidU.ToString());
connU.Open();
cmdU.ExecuteNonQuery();
connU.Close();
}
}
catch(异常例外)
{
SPEventLog(其他文档GUID值错误",例如消息,属性);
}
}
}
}
谢谢
嗨
您可以通过vs附加线程,然后调试代码.
SharePoint线程名为w3wp.
为您提供一些屏幕截图:
附加线程
Guys, This is my requirement.
i have to update a sql table whenever user creates/updates/ deletes a record in SP List.
this is a simple list with 4 columns ( one of them is a lookup column )
i have created an event receiver: item added, delete works but update is not working.
i want to delete and insert rows when someone edits a record ( as this has multiple lookup value selection column ).
below is my code, only insert works from this not the delete and i could not find specific reason why it is behaving like this.
any help appreciated.
CODE:
public override void ItemUpdated(SPItemEventProperties properties){
//base.ItemUpdated(properties);
SPWeb web = properties.OpenWeb();
SPList list = web.Lists["Misc Doc Divisions"];
SPListItem item;
mdlistitemID = properties.ListItem.UniqueId.ToString();
SPFieldLookupValueCollection lValues = properties.ListItem["Associated_x0020_Division_x0020_"] as SPFieldLookupValueCollection;
SPFieldLookupValueCollection lValuesI = properties.ListItem["Associated_x0020_Division_x0020_"] as SPFieldLookupValueCollection;
count = lValues.Count;
SPEventLog("Updated", "Count: " + count + "lValues[0]" +lValues[0], properties);
foreach (SPFieldLookupValue lvalue in lValues)
{
lookupListItemIDU = lvalue.LookupId;
lookupListItemValueU = lvalue.LookupValue;
item = list.GetItemById(lookupListItemIDU);
guidU = item.UniqueId;
try
{
using (SqlConnection connD = new SqlConnection())
{
connD.ConnectionString = "<Connection String>";
SqlCommand cmd = new SqlCommand("DELETE FROM <Table Name> WHERE MiscDocumentsListItemID = @mdLiID and MiscDocDivisionsListItemID = @mddLiID ", connD);
cmd.Parameters.AddWithValue("@mdLiID", mdlistitemID);
cmd.Parameters.AddWithValue("@mddLiID", guidU.ToString());
connD.Open();
cmd.ExecuteNonQuery();
connD.Close();
}
}
catch (Exception ex)
{
SPEventLog("MD2D Delete Error", ex.Message, properties);
}
}
foreach (SPFieldLookupValue lvalueI in lValuesI)
{
lookupListItemIDU = lvalueI.LookupId;
lookupListItemValueU = lvalueI.LookupValue;
item = list.GetItemById(lookupListItemIDU);
guidU = item.UniqueId;
try
{
using (SqlConnection connU = new SqlConnection())
{
connU.ConnectionString = "<Connection String>";
SqlCommand cmdU = new SqlCommand("INSERT INTO <Table Name>([MiscDocumentsListItemID],[MiscDocDivisionsListItemID]) VALUES (@mdLiID, @mddLiID)", connU);
cmdU.Parameters.AddWithValue("@mdLiID", mdlistitemID);
cmdU.Parameters.AddWithValue("@mddLiID", guidU.ToString());
connU.Open();
cmdU.ExecuteNonQuery();
connU.Close();
}
}
catch (Exception ex)
{
SPEventLog("Misc Doc GUID Value Ins Error", ex.Message, properties);
}
}
}
}
Thanks
Hi
You can attach the thread by vs, then debug the code.
SharePoint thread is named w3wp.
Some screen capture for you:
attach thread
这篇关于SharePoint ItemUpdated事件接收器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!