我在 Visual Studio 2010 for SharePoint 2010 中遇到了事件处理程序 (Visual C) 的问题 - 我要做的就是将列表从一个网站集复制到另一个网站集。我在代码中有标记点来写入日志文件以监视进度/访问,它读取并计算另一个站点上的列表项并写入日志文件,一切正常,除了它没有更新另一个列表?
请看下面的代码:
/// --------------------------------------------------------------
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Text;
using System.IO;
namespace MyCustomListener2013.MyListEventReceiver
{
public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
/// --------------------------------------------------------------
/// Validate current site
/// --------------------------------------------------------------
using (SPSite site = properties.OpenSite())
{
/// --------------------------------------------------------------
/// Open current site and check access
/// --------------------------------------------------------------
StreamWriter sw1 = new StreamWriter("log.txt");
sw1.WriteLine("SiteOpened");
sw1.Close();
using (SPWeb web = site.OpenWeb())
{
/// --------------------------------------------------------------
/// Open current web and check access
/// --------------------------------------------------------------
StreamWriter sw2 = new StreamWriter("log.txt");
sw2.WriteLine("WebOpened");
sw2.Close();
web.AllowUnsafeUpdates = true;
/// --------------------------------------------------------------
/// Open current list and check access
/// --------------------------------------------------------------
SPList list = web.Lists["Cust_1"];
SPItem item = list.Items.Add();
StreamWriter sw3 = new StreamWriter("log.txt");
string s = list.ItemCount.ToString();
sw3.WriteLine(s);
sw3.Close();
}
}
/// --------------------------------------------------------------
/// Validate other site
/// --------------------------------------------------------------
using (SPSite site = new SPSite("http://srvvoidapp01:9002/Lists/Cust_1/Allitems.aspx"))
{
/// --------------------------------------------------------------
/// Open other site and check access
/// --------------------------------------------------------------
StreamWriter sw1 = new StreamWriter("log2.txt");
sw1.WriteLine("SiteOpened");
sw1.Close();
using (SPWeb web = site.OpenWeb())
{
/// --------------------------------------------------------------
/// Open other web and check access
/// --------------------------------------------------------------
StreamWriter sw2 = new StreamWriter("log2.txt");
sw2.WriteLine("WebOpened");
sw2.Close();
/// --------------------------------------------------------------
/// Check other list and count list items to log file
/// --------------------------------------------------------------
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Cust_1"];
SPItem item = list.AddItem();
StreamWriter sw3 = new StreamWriter("log2.txt");
string s = list.ItemCount.ToString();
sw3.WriteLine(s);
sw3.Close();
/// ---------------------------------------------------------------------
/// Update new entry on Cust_1 list from site A to Cust_1 list on site B
/// ---------------------------------------------------------------------
item["Title"] = properties.ListItem["Title"];
item["col_1"] = properties.ListItem["col_1"];
item["col_2"] = properties.ListItem["col_2"];
item.Update();
list.Update();
web.AllowUnsafeUpdates = false;
/// --------------------------------------------------------------
/// Confirm update to log file
/// --------------------------------------------------------------
StreamWriter sw4 = new StreamWriter("log2.txt");
sw4.WriteLine("OtherListUpdated");
sw4.Close();
}
}
}
任何输入/帮助将不胜感激。由于 SharePoint 工作流(SP 设计器)无法跨网站集进行写入,因此每次在当前列表中添加新项目时,我都需要一个事件处理程序来更新另一个列表。
谢谢!