locked
How to restore a file version in sharepoint? RRS feed

  • Question

  • hi,guys
       how to restore a file version in sharepoint?
    I believe I can!
    Thursday, July 23, 2009 8:35 AM

Answers

  • Here is an example code. It should restore the files to their first versions. Extend the example according to your needs.

    EDIT: Just not sure if you really need the UnsafeUpdates and the item Update. But the code works.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace TestApp
    {
        class Program
        {
            public static SPWeb Web;
            public const string LIST_NAME = "Some List Name";
    
            static void Main(string[] args)
            {
                SPSite site = new SPSite("http://server:port/");
    
                Web = site.RootWeb;
    
                SPListItemCollection items = Web.Lists[LIST_NAME].Items;
    
                foreach (SPListItem item in items)
                {
                    Web.AllowUnsafeUpdates = true;
                    item.Versions.RestoreByID(item.Versions[0].VersionId);
                    item.Update();
                    Web.AllowUnsafeUpdates = false;
                }
            }
        }
    }
    


    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    • Proposed as answer by Hristo Yankov Thursday, July 23, 2009 9:00 AM
    • Edited by Hristo Yankov Thursday, July 23, 2009 9:01 AM Unsafe Updates, item Update
    • Marked as answer by Aaron Han - MSFT Monday, July 27, 2009 9:50 AM
    Thursday, July 23, 2009 8:58 AM

All replies

  • Hi, if you want to restore a previos file version on a document library, you can go to DispForm page of the document you want, and then click on version history, if you put the mouse over one version, anc click on, a menu it´s showed and then click on restore.
    Microsoft MCP 6608628
    Thursday, July 23, 2009 8:49 AM
  • Here is an example code. It should restore the files to their first versions. Extend the example according to your needs.

    EDIT: Just not sure if you really need the UnsafeUpdates and the item Update. But the code works.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace TestApp
    {
        class Program
        {
            public static SPWeb Web;
            public const string LIST_NAME = "Some List Name";
    
            static void Main(string[] args)
            {
                SPSite site = new SPSite("http://server:port/");
    
                Web = site.RootWeb;
    
                SPListItemCollection items = Web.Lists[LIST_NAME].Items;
    
                foreach (SPListItem item in items)
                {
                    Web.AllowUnsafeUpdates = true;
                    item.Versions.RestoreByID(item.Versions[0].VersionId);
                    item.Update();
                    Web.AllowUnsafeUpdates = false;
                }
            }
        }
    }
    


    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    • Proposed as answer by Hristo Yankov Thursday, July 23, 2009 9:00 AM
    • Edited by Hristo Yankov Thursday, July 23, 2009 9:01 AM Unsafe Updates, item Update
    • Marked as answer by Aaron Han - MSFT Monday, July 27, 2009 9:50 AM
    Thursday, July 23, 2009 8:58 AM
  • thanks, but I want to do it by using code
    I believe I can!
    Friday, July 24, 2009 5:49 AM
  • thank you ,Hristo,it's very userful for me!
    I believe I can!
    Friday, July 24, 2009 5:50 AM