How to Download to XML files Previous Versions of

2019-07-19 15:47发布

问题:

When i access the list of Infopath files, and select one for example and click the Version History, is there a way to download previous versions without Restoring the old versions?

回答1:

This will only work for items in libraries (and wont work on lists). Some points to remember:

  • Libraries have properties called Files
  • Files have properties called versions
  • Versions have a property called Url

With those three in mind, you can pull the download URL for all the version history of the InfoPath file by doing the following:

public static void Main(string[] args)
    {
        ClientContext context = new ClientContext(<sharepoint site here>);
        Web site = context.Web;
        List docLib = site.Lists.GetByTitle(<doc lib here>);
        context.Load(docLib);

        CamlQuery caml = new CamlQuery();
        ListItemCollection items = docLib.GetItems(caml);

        context.Load(items);
        context.ExecuteQuery();

        Console.WriteLine("Pulling information...");

        foreach(ListItem item in items)
        {
            //You can change title to any internal field name that you want to use as basis
            if (item.FieldValues["Title"] != null)
            {
                Console.WriteLine("File Name: " + item.FieldValues["Title"]);
                context.Load(item);
                //this gets ALL the version of the file
                FileVersionCollection versions = item.File.Versions;

                context.Load(versions);
                context.ExecuteQuery();

                if (versions != null)
                {
                    foreach (FileVersion version in versions)
                    {
                        User usr = version.CreatedBy as User;
                        context.Load(usr);
                        context.ExecuteQuery();

                        //will be explained in detail
                        int ver = GetVersion(version.VersionLabel);
                        //will be explained in detail
                        int verLink = ver * 512;
                        string link = "your sharepoint site";
                        //Console.WriteLine("Version Info:: {0}, {1}, {2}, {3}", version.VersionLabel, version.Created, usr.LoginName, version.CheckInComment);
                        Console.WriteLine("Document Link: " + link + version.Url);
                        Console.WriteLine("Version: " + ver.ToString());
                        Console.WriteLine("Created: " + version.Created);
                        Console.WriteLine("Created By: " + usr.LoginName);
                        Console.WriteLine("Comments: " + version.CheckInComment);
                    }
                }
            }
        }

        Console.WriteLine("Pulling information complete.");
        Console.Read();
    }

Notice that I used a method called GetVersion? This is a simple method which just gets the major version # of a document. It looks like this:

public static int GetVersion(string itemVersion)
    {
        int index = itemVersion.IndexOf('.');
        return int.Parse(itemVersion.Substring(0, index));
    }

If you will be needing the minor version you can just create another method to pull that information. What this method is used for is to generate the download link you need for each document version. As for the details on why we need to multiply the version to 512, you can read it up on here.

Hope this helps. :)