none
trying to use .copyTo method RRS feed

  • Question

  • Hi Everyone,

     

    I wrote an event handler that I am testing out. I tried to create one that after editing a task item, it gets copied to another list on the same site, very basic, I'm just trying to get a sense of working with event handlers. The item gets updated but does not get copied over to the other task list. Can someone see what I am doing wrong with my code?

     

    (Ignore the namespace and class names, I was originally testing out how to prevent deletion of my task items)

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;

    namespace DeletingEventHandler
    {
        public class DeletingAction : SPItemEventReceiver
        {
            public override void ItemUpdated(SPItemEventProperties properties)
            {
                SPListItem taskItem = properties.ListItem;
                
                this.DisableEventFiring();
                taskItem.CopyTo("http://myServer/mySubSite/Lists/Tasks%20Two/AllItems.aspx");
                taskItem.Update();
                this.EnableEventFiring();

            }
        }
    }

    Tuesday, November 27, 2007 12:38 AM

Answers

  • Well hold on.  Is this other list named "GUID" or by "GUID" do you mean that you supplied the actual GUID of the list as a string to web.Lists[] ?

     

    Make sure to update the list as well.  Add a call to web.Lists["GUID" or GUID].Update();

     

    Also, since debugging in SharePoint sucks, start dumping things out to text files.  Check if item is null, check if the "other" list it null, check the values of the ListItem's fields.  Write this down to a text file to see what's going on.  So let's be even MORE explicit:

     

    public override void ItemUpdated(SPItemEventProperties properties)

    {

    //open site

    using (SPWeb web = properties.OpenWeb())

    {

    //get "other" list and create new item there

    SPList list = web.Lists["GUID"];

    if (list == null)

    {

    //write this to a file

    WriteToFile("List is null");

    }

     

    SPListItem item = list.Items.Add();

     

    //copy fields

    WriteToFile(properties.AfterProperties["name of field"]);

    WriteToFile(properties.BeforeProperties["name of field"]);

    WriteToFile(properties["name of field"]);

    item["name of field"] = properties.ListItem["name of field"];

     

    //save

    item.Update();

    list.Update();

    }

    }

    Tuesday, November 27, 2007 7:17 PM

All replies

  • Hey DHX

     

    Maybe I'm a little oldschool, but that CopyTo method seems like everything in the two lists (schema, ID's, etc) has to be fairly insync.  I don't like it.

     

    Since you're just messing around anyways, write some more explicit code to see what's going on, ie:

     

    namespace ClassLibrary1

    {

    public class Class1 : SPItemEventReceiver

    {

    public override void ItemUpdated(SPItemEventProperties properties)

    {

    //open site

    using (SPWeb web = properties.OpenWeb())

    {

    //get "other" list and create new item there

    SPListItem item = web.Lists["name of list"].Items.Add();

     

    //copy fields

    item["name of field"] = properties.ListItem["name of field"];

     

    //save

    item.Update();

    }

    }

    }

    }

     

    If this works, then CopyTo is being a butt.  If not, then check your URL, or read up on EXACTLY what CopyTo does.  Hit me back and let me know.

    Tuesday, November 27, 2007 5:23 PM
  • Same problem, I update the item, the item gets updated but nothing gets copied to the other list. The lists are identical tasks lists. I even used the GUID for the name of the second list (my code is below):

     

    namespace UpdatedEventHandler

    {

    public class UpdateAction : SPItemEventReceiver

    {

    public override void ItemUpdated(SPItemEventProperties properties)

    {

     

    using (SPWeb web = properties.OpenWeb())

    {

     

    SPListItem item = web.Lists["GUID"].Items.Add();

    item["Title"] = properties.ListItem["Title"];

    item["Priority"] = properties.ListItem["Priority"];

    item.Update();

    }

    }

    }

    }

     

    Tuesday, November 27, 2007 7:06 PM
  • Well hold on.  Is this other list named "GUID" or by "GUID" do you mean that you supplied the actual GUID of the list as a string to web.Lists[] ?

     

    Make sure to update the list as well.  Add a call to web.Lists["GUID" or GUID].Update();

     

    Also, since debugging in SharePoint sucks, start dumping things out to text files.  Check if item is null, check if the "other" list it null, check the values of the ListItem's fields.  Write this down to a text file to see what's going on.  So let's be even MORE explicit:

     

    public override void ItemUpdated(SPItemEventProperties properties)

    {

    //open site

    using (SPWeb web = properties.OpenWeb())

    {

    //get "other" list and create new item there

    SPList list = web.Lists["GUID"];

    if (list == null)

    {

    //write this to a file

    WriteToFile("List is null");

    }

     

    SPListItem item = list.Items.Add();

     

    //copy fields

    WriteToFile(properties.AfterProperties["name of field"]);

    WriteToFile(properties.BeforeProperties["name of field"]);

    WriteToFile(properties["name of field"]);

    item["name of field"] = properties.ListItem["name of field"];

     

    //save

    item.Update();

    list.Update();

    }

    }

    Tuesday, November 27, 2007 7:17 PM
  • Thank you very much, your code helped me out a LOT!! I think my problem was that I modified my event handler code but still had the old version cached in sharepoint. So it was looking for my ItemDeleted handler after I had changed it to ItemUpdated handler. I cleaned up everything using stsadm and it worked.

     

    thanks again

     

    dhx

     

    Wednesday, November 28, 2007 7:24 PM