在 Sharepoint 中,如何将列表项从一个列表复制到另一个列表 例如从“List A”复制到“List B”(两者都在站点的根目录中)
我希望在将新列表项添加到“列表 A”时进行此复制
我尝试在 ItemAdded 事件接收器中使用 SPListItem 的 CopyTo() 方法,但无法找出要复制到的 url。
最佳答案
这是我使用的代码。将一个 SPlistItem 和目标列表的名称传递给它,如 Sharepoint 中所示(不是 URL)。唯一的限制是两个列表必须在同一站点中:
private SPListItem CopyItem(SPListItem sourceItem, string destinationListName) {
//Copy sourceItem to destinationList
SPList destinationList = sourceItem.Web.Lists[destinationListName];
SPListItem targetItem = destinationList.Items.Add();
foreach (SPField f in sourceItem.Fields) {
//Copy all except attachments.
if (!f.ReadOnlyField && f.InternalName != "Attachments"
&& null != sourceItem[f.InternalName])
{
targetItem[f.InternalName] = sourceItem[f.InternalName];
}
}
//Copy attachments
foreach (string fileName in sourceItem.Attachments) {
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}
return targetItem;
}
关于c# - 在 Sharepoint 中将列表项从一个列表复制到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1075323/