Azra
Azra

Reputation: 123

How to copy SPListitem from one SPList to Another SPList

I have requirement to copy items from one SPList to another,

Here is the code which is not working:

public void CopyList(SPList src)
{
    //Copy items from source List to Destination List
    foreach (SPListItem item in src.Items)
    {
        if(isUnique(item.UniqueId))
        {
            foreach (SPField field in src.Fields)
            {
               try
               {
                    if (!field.ReadOnlyField)
                        newDestItem = DestinationList.Items.Add();
                    newDestItem[field.Id] = item[field.Id];
                    newDestItem.Update();
               }
               catch (Exception ex)
               {
                   ex.ToString();
               }
            }
            //newDestItem["wrkspace"] = src.ParentWeb.Name;
            // newDestItem.Update();
        }
        DestinationList.Update();  
    }
    // DestinationList.Update();
}

Upvotes: 1

Views: 20299

Answers (4)

Jeroen Ritmeijer
Jeroen Ritmeijer

Reputation: 2792

You forgot to copy the item's attachments. Have a look at this article, part of the code has been repeated below.

// ** Copy the fields
foreach(SPField field in sourceItem.Fields)
{
    if (newItem.Fields.ContainsField(field.InternalName) == true && 
        field.ReadOnlyField == false && field.InternalName != "Attachments")
    {
       newItem[field.InternalName] = sourceItem[field.InternalName];
    }
}

// ** Delete any existing attachments in the target item
for (int i = newItem.Attachments.Count; i > 0; i-- )
{
    newItem.Attachments.Delete(newItem.Attachments[i-1]);
}

// ** Copy any attachments
foreach (string fileName in sourceItem.Attachments)
{
    SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + 
                                                          fileName);
    byte[] imageData = file.OpenBinary();
    newItem.Attachments.Add(fileName, imageData);
}

// ** Remember where the original was copied from so we can update it in the future
newItem["_M_CopySource"] = sourceItem["FileRef"];

newItem.Update();

Upvotes: 3

naivists
naivists

Reputation: 33511

  1. Do not call "update" on DestItem on every field. You only need it once!
  2. field.id may be different in different lists. Use the InternalName property instead.
  3. The exception you catch is not saved anywhere!
  4. You do not have to call DestionationList.Update, you are not changing the destination list's settings or anything.

I modified the code to show these changes

public void CopyList(SPList src) 
{ 
    //Copy items from source List to Destination List 
    foreach (SPListItem item in src.Items) 
    { 
        if(isUnique(item.UniqueId)) 
        { 
          newDestItem = DestinationList.Items.Add(); 

          foreach (SPField field in src.Fields) 
          { 
             try 
              { 
                if ((!field.ReadOnlyField) && (field.InternalName!="Attachments"))
                  newDestItem[field.InternalName] = item[field.InternalName]; 
               } 
             catch (Exception ex) 
              { 
              //you should save the "ex" somewhere to see its outputs
               ex.ToString(); 
              } 
           } 
           newDestItem.Update();  //only now you call update!
        } 
       } 
      } 

Upvotes: 0

jaloplo
jaloplo

Reputation: 951

Look at this post, link. This is the best approach I found.

Upvotes: 0

Rob Windsor
Rob Windsor

Reputation: 6859

The SPListItem type has a CopyTo method that will do what you want.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx

Upvotes: 3

Related Questions