1

我在 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 设计器)无法跨网站集进行写入,因此每次在当前列表中添加新项目时,我都需要一个事件处理程序来更新另一个列表。

谢谢!

4

1 回答 1

2

起初你不应该每次都初始化当前站点和网站。使用 properties.Web 获取 SPWeb 对象。同样的方式你可以得到 SPList 和 SPItem 对象:properties.Listproperties.ListItem. web.AllowUnsafeUpdates = true您应该在更新列表项时使用。您将项目添加到另一个网站集列表的代码应如下所示:

   public override void ItemAdding(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);


       string s1 = properties.List.ItemCount.ToString();
       WriteLog(s1);
       using (SPSite site = new SPSite("http://srvvoidapp01:9002/"))
       {

           WriteLog("SiteOpened");

          using (SPWeb web = site.OpenWeb())
          {


            WriteLog("WebOpened");

            web.AllowUnsafeUpdates = true;

            SPList list = web.GetList(web.ServerRelativeUrl.TrimEnd('/') + "/Lists/Cust_1");
            SPItem item = list.AddItem();

            string s2 = list.ItemCount.ToString();

            WriteLog(s2);

            item.Title = properties.ListItem.Title;
            item["col_1"] = properties.ListItem["col_1"];
            item["col_2"] = properties.ListItem["col_2"];
            item.UpdateOverwriteVersion();

            web.AllowUnsafeUpdates = false;

            WriteLog("OtherListUpdated");

           }
        }

   }

   private void WriteLog(String s)
   {
      using (StreamWriter sw = new StreamWriter("log2.txt"))
      {
        sw.WriteLine(s);
      }
   }
于 2013-11-05T12:05:12.927 回答