使用c#将文档库项从一个文件夹移动到另一个文件夹,包括版本历史

时间:2011-04-26 17:41:33

标签: sharepoint documents

使用Sharepoint 2007对象模型,我一直在寻找C#中的一个示例,将项目从一个文档库移动到同一服务器上的另一个文档库并保存版本历史记录(即SPListItemVersion对象)和元数据(文件夹具有相同的内容类型等)。

1 个答案:

答案 0 :(得分:1)

我能够通过以下代码完成我想要做的事情:

    /// <summary>  
    /// Adds item to archive  
    /// </summary>  
    /// <param name="item">Item to add</param>  
    /// <param name="destination">Archive path</param>  
    /// <param name="destination">web site of archive</param>  
    /// <returns>Result of arhivation process</returns>  
    public static string ArchiveItem(SPListItem item, string destination, SPWeb web)
    {
        // Save main meta information for later use:  
        var author = item.File.Author;
        var modifiedBy = item.File.ModifiedBy;
        var modified = item.File.TimeLastModified;

        var created = item.File.TimeCreated;
        // Get destination filename:  
        var destinationFile = destination + "/" + item.File.Name;
        // Copy the item and set properties:  
        var coppiedFile = web.GetFolder(destination).Files.Add(destinationFile, item.File.OpenBinary(), author, modifiedBy, created, modified);
        coppiedFile.Item["Created"] = created;
        coppiedFile.Item["Modified"] = modified;
        // Save changes, UpdateOverwriteVersion causes object to save without saving a new version.
        coppiedFile.Item.UpdateOverwriteVersion();
        // If moving is enabled, delete original item:  
        item.Delete();

        return coppiedFile.ServerRelativeUrl;
    }