How to copy SPListitem from one SPList to Another SPList

后端 未结 4 1085
野的像风
野的像风 2021-01-25 09:39

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 f         


        
4条回答
  •  一生所求
    2021-01-25 10:19

    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!
            } 
           } 
          } 
    

提交回复
热议问题