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

发布于 2024-07-27 17:19:59 字数 901 浏览 10 评论 0原文

我需要将项目从一个 SPList 复制到另一个 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁对谁错谁最难过 2024-08-03 17:19:59

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

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

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

酷炫老祖宗 2024-08-03 17:19:59

您忘记复制该项目的附件。 看看这篇文章,下面重复了代码。

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

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();
耳根太软 2024-08-03 17:19:59

看看这篇文章,

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

乖乖兔^ω^ 2024-08-03 17:19:59
  1. 不要对 DestItem 调用“更新”
    在每个领域。 你只需要它
    一次!
  2. 不同列表中的field.id可能不同。 请改用 InternalName 属性。
  3. 您捕获的异常不会保存在任何地方!
  4. 您不必调用 DestionationList.Update,您不会更改目标列表的设置或任何内容。

我修改了代码以显示这些更改

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!
        } 
       } 
      } 
  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!
        } 
       } 
      } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文