SharePoint 2010事件接收器更新另一个列表(在另一个网站集中)

时间:2013-11-05 10:36:47

标签: visual-studio-2010 events sharepoint event-handling

我在Visual Studio 2010 for SharePoint 2010中遇到了事件处理程序(Visual C)的问题 - 我所要做的就是将列表从一个网站集复制到另一个网站集。我在代码中有标记点写入日志文件以监视进度/访问,它读取并计算其他站点上的列表项并写入日志文件,一切正常,但是,它没有更新其他列表?? < / p>

请查看以下代码:

/// --------------------------------------------------------------     

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

谢谢!

1 个答案:

答案 0 :(得分: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);
      }
   }