开发者

Moving a document library item from one folder to another including version history using c#

Using Sharepoint 2007 object model, I have been looking for an example in C# to move an item from one document library to another on the same server and saving the version history (i.e. SPListItemVersion obje开发者_运维百科cts) and metadata (the folders have the same content types, etc).


I was able to accomplish what I wanted to do with the following code:

    /// <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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜