在Sharepoint中如何将列表项从一个列表复制到另一个列表,例如从"列表A"复制到"列表B"(两者都位于站点的根目录下)
我希望在将新列表项添加到"列表A"时进行此复制
我尝试在ItemAdded事件接收器中使用SPListItem的CopyTo()方法,但无法找出要复制到的url.
小智 17
这是我使用的代码.传递一个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;
}
Run Code Online (Sandbox Code Playgroud)
事实上,正如Lars所说,移动项目并保留版本和更正用户信息可能很棘手.我之前做过类似的事情,所以如果你需要一些代码示例,请通过评论告诉我,并为你提供一些指导.
该CopyTo从方法(如果你决定去与)需要一个绝对URI,如: HTTP://host/site/web/list/filename.doc
因此,如果您在事件接收器中执行此操作,则需要连接包含所需元素的字符串.类似的东西(请注意,这可以通过其他方式完成):
string dest=
siteCollection.Url + "/" + site.Name + list.Name + item.File.Name;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76691 次 |
| 最近记录: |