c# - 在 Sharepoint 中将列表项从一个列表复制到另一个列表

标签 c# sharepoint

在 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/

相关文章:

c# - 根据模型中的枚举设置选中的单选按钮

javascript - 使用按钮加载 View 单击新页面 MVC

javascript - 通过javascript获取两个字符串之间的字符串值

javascript - SharePoint `sender` 和 `args` 是什么?

c# - 除了 "await"之外还有哪些情况会允许同步代码被中断

c# - 电子商务网站的不同搜索结果

c# - 如何显示 "Open with"文件对话框?

powershell - Azure 函数中的 PNP PowerShell 在某个时间间隔内快速触发时失败

sharepoint - 在 Windows 7 机器上安装 Microsoft SharePoint 2010 时出错

sharepoint - 在 Sharepoint 上使用 wiki 工具的客观原因?