首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将SPListitem从一个SPList复制到另一个SPList

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

Stack Overflow用户
提问于 2009-07-17 08:26:26
回答 4查看 20K关注 0票数 1

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

下面是不起作用的代码:

代码语言:javascript
复制
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();
}
EN

回答 4

Stack Overflow用户

发布于 2009-07-17 08:42:45

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

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

票数 3
EN

Stack Overflow用户

发布于 2010-01-29 00:42:11

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

代码语言:javascript
复制
// ** 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();
票数 3
EN

Stack Overflow用户

发布于 2010-01-07 19:27:50

看看这篇文章,link。这是我找到的最好的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1142014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档