1
votes

Ive got a issue with event handlers (Visual C) in Visual Studio 2010 for SharePoint 2010 - all I am trying to do is to duplicate a list from one site collection to another. I have flagging points in the code to write to a log file to monitor progress/access, it reads and counts the list items on the other site and writes to log file, everything works, except, its not updating the other list??

Please look at code below:

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

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();

             }
            }

       }

Any input/assistance would be appreciated. As SharePoint workflows (SP Designer) cannot write across site collections, I need an event handler to update another list each time a new item is added in the current list.

Thanks!

1

1 Answers

2
votes

At first you shouldn't init current site and web each time. Use properties.Web to get SPWeb object. Same way you can get SPList and SPItem objects: properties.List and properties.ListItem. web.AllowUnsafeUpdates = true you should to use when you update list item. Your code to add item to list at another site collection should be look like this:

   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);
      }
   }