如何将SPListitem从一个SPList复制到另一个SPList

时间:2009-07-17 08:26:26

标签: sharepoint splistitem

我要求将项目从一个SPList复制到另一个,

以下是无效的代码:

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

4 个答案:

答案 0 :(得分:3)

SPListItem类型有一个CopyTo方法,可以执行您想要的操作。

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

答案 1 :(得分:3)

您忘记复制该项目的附件。看看this article,下面重复了部分代码。

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

答案 2 :(得分:0)

看看这篇文章link。这是我发现的最佳方法。

答案 3 :(得分:0)

  1. 不要在DestItem上调用“update” 在每个领域。你只需要它 一旦!
  2. field.id在不同的列表中可能有所不同。请改用InternalName属性。
  3. 您捕获的异常不会保存在任何地方!
  4. 您无需致电DestionationList.Update,也不会更改目的地列表的设置或任何内容。
  5. 我修改了代码以显示这些更改

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