如何将 SPListitem 从一个 SPList 复制到另一个 SPList [英] How to copy SPListitem from one SPList to Another SPList
本文介绍了如何将 SPListitem 从一个 SPList 复制到另一个 SPList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将项目从一个 SPList 复制到另一个,
I have requirement to copy items from one SPList to another,
这是不起作用的代码:
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();
}
推荐答案
您忘记复制项目的附件.看看这篇文章,部分下面重复了代码.
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();
这篇关于如何将 SPListitem 从一个 SPList 复制到另一个 SPList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文