如何以编程方式将SPListItem从源列表移动到目标列表(到列表的特定文件夹)?

时间:2019-05-28 09:47:32

标签: c# sharepoint sharepoint-2013 splistitem splist

我在SharePoint中非常陌生(我正在使用SharePoint 2013),并且遇到以下问题:

我在来源网站上有一个来源列表(一个 SPList ),我必须将其项目移动到目的地列表(另一个 SPList )。

因此,基本上,我必须将一些 SPListItem 从列表移动到两个不同网站上的另一个列表。

更复杂的是,目标列表包含基于迁移项目上日期字段的文件夹(例如:如果日期为:2019/05/28,它将在目标列表中创建以下文件夹,如下所示) 2019-> 05-> 28,其中必须放置此 SPListItem )。

我知道我可以做这样的事情:

private static void copyAttachments(SPListItem sourceItem, SPList sourceAttachList, SPList destAttachList, string destUrl)
{
    // Create the destination item for the attachments:
    SPListItem destAttachItem = null;

    string recNumber = sourceItem[Arxeia6Fields.NumeroProtocollo].ToString();
    DateTime recDate = (DateTime)sourceItem[Arxeia6Fields.DataProtocollo];

    /*
     * Add an item in a specific server-relative URL of the folder where the list item should be created.
     * The new list item represents a file.
     */
    destAttachItem = destAttachList.AddItem(destUrl, SPFileSystemObjectType.File);
    string title = recNumber + "/" + recDate.Year.ToString();
    destAttachItem["Title"] = title;
    destAttachItem["Numero protocollo"] = title;

}

使用此代码,我将创建一个新项目添加到目标列表(名为 destAttachList ,指定 destUrl 代表此列表中放置该项目的确切文件夹(我(从上一个处理步骤中获得此信息)。然后,我只需使用我要迁移的源列表的项目的值来设置目标列表中该项目的2个字段的值即可。

我的疑问是:

  1. 我可以将项目从源列表移动到目标列表(在目标URL指定的特定文件夹中)吗?

  2. 如果迁移中的该项目包含附件,那么可以通过此单个移动步骤自动迁移这些附件吗? (如果可能)

1 个答案:

答案 0 :(得分:0)

您不必手动执行所有这些操作,因为SharePoint已经开箱即用地实现了此方法。您甚至不需要其他方法,就应该能够通过调用MoveTo方法来实现所有这些功能。

sourceItem.File.MoveTo(SPUrlUtility.CombineUrl(destinationList.ParentWeb.Url,destinationList.RootFolder.Url));

当然,sourceItem是您要移动的SPListItemdestinationList是应该作为SPListItem目的地的SPList