Pages

Tuesday, March 6, 2012

How to Copy ListItem from One List to Another List

Hello

Here is code how to copy ListItem from one list to another list from eventhandler.
Make sure both list has same List Field Structure.

Here is Code

public override void ItemAdded(SPItemEventProperties properties)
       {

           if (properties.List.Title.Equals("ListA"))
           {
               SPListItem CopyItem = properties.Web.Lists["ListB"].Items.Add();

               foreach (SPField f in properties.ListItem.Fields)
               {
                   //Copy all except attachments.
                   if (!f.ReadOnlyField && f.InternalName != "Attachments"
                       && null != properties.ListItem[f.InternalName])
                   {
                       CopyItem[f.InternalName] = properties.ListItem[f.InternalName];
                   }
               }


               foreach (string fileName in properties.ListItem.Attachments)
               {
                   SPFile file = properties.ListItem.ParentList.ParentWeb.GetFile(properties.ListItem.Attachments.UrlPrefix + fileName);
                   byte[] imageData = file.OpenBinary();
                   CopyItem.Attachments.Add(fileName, imageData);
               }
               CopyItem.Update();

           }
       }

Hope it helps!!!

Disha Shah

7 comments:

  1. thanks Disha it's work well
    I have another problem
    this time i just want to copy the column of a listA wich empty(copy just name of column for exemple if my listA have 3 Colunm named title,name,date another listB will be created automatically with the same name of colunm with listA (using eventhandler)
    thanks

    ReplyDelete
    Replies
    1. Hi Mouna

      Please add below code After this line:
      SPListItem CopyItem = properties.Web.Lists["ListB"].Items.Add();

      Code to copy Source List fields which are not in destination list :
      Code
      ----------
      foreach (SPField notcontain in properties.ListItem.Fields)
      {
      if (!properties.Web.Lists["ListB"].Fields.ContainsField(notcontain.Title))
      {
      properties.Web.Lists["ListB"].Fields.Add(notcontain);
      properties.Web.Lists["ListB"].Update();
      }
      }

      Thanks
      Disha Shah

      Delete
    2. Thanks Disha,
      i have thousand and thousand of questions but i m so a shamed
      every time you save my life
      if you can help me i want customized the quick launch menu in this way:
      i have three link

      what i want when i click on "Lien principale 1" for exemple i obtain:

      Lien principale 1
      lien interne 1
      lien interne 2
      lien interne 3
      Lien principale 2
      Lien principale 3

      another click on "Lien principale 1" this interne link hiding:
      Lien principale 1
      Lien principale 2
      Lien principale 3

      any suggestions

      Delete
    3. Hi Mouna

      you could do those things via Quick Launch Settings from Site Settings.

      May be this Link will help you what you are trying to achieve
      http://community.bamboosolutions.com/blogs/sharepoint_blank/archive/2009/05/20/customize-the-sharepoint-quick-launch-part-1-how-to-add-a-quick-launch-heading.aspx

      If you still have questions please let me know , I would be happy to help you out.

      Thanks
      Disha Shah

      Delete
  2. Hi Disha,
    I am new to sharepoint development
    on Quick launch menu i have this structure:
    MANAGEMENT DE PROJET
    gestion de projet
    projet ET000
    projet ET011
    what i want to do is:
    when I click on "gestion de projet" others links appears just under "gestion de projet" juste like your "Blogs Archive".
    Thanks in advance,

    ReplyDelete
    Replies
    1. Hi Emna

      you could do those things via Quick Launch Settings from Site Settings.

      May be this Link will help you what you are trying to achieve
      http://community.bamboosolutions.com/blogs/sharepoint_blank/archive/2009/05/20/customize-the-sharepoint-quick-launch-part-1-how-to-add-a-quick-launch-heading.aspx

      If you still have questions please let me know , I would be happy to help you out.

      Thanks
      Disha Shah

      Delete
  3. Hi Disha,
    another problem:
    what i want to do:
    when i click on a buttom in the ribbon ,a link will be automatically created in the quick launch menu and 2 others will be deleted
    any suggestions please
    thanks

    ReplyDelete