如何将 SPListitem 从一个 SPList 复制到另一个 SPList
我需要将项目从一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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
您忘记复制该项目的附件。 看看这篇文章,下面重复了代码。
You forgot to copy the item's attachments. Have a look at this article, part of the code has been repeated below.
看看这篇文章,
Look at this post, link. This is the best approach I found.
在每个领域。 你只需要它
一次!
field.id
可能不同。 请改用InternalName
属性。DestionationList.Update
,您不会更改目标列表的设置或任何内容。我修改了代码以显示这些更改
on every field. You only need it
once!
field.id
may be different in different lists. Use theInternalName
property instead.DestionationList.Update
, you are not changing the destination list's settings or anything.I modified the code to show these changes