0

I need to download the Older-Minor version of a File(.docx/.excl etc) inside the SharePoint folder using REST API. The URL would be: https://BaseURL/sites/SubSite/_api/web/GetFileByServerRelativeUrl('/sites/SubSite/Documents/New Folder/xyz.docx')/versions(1)/$value.

When I try to download the Major Versions the file gets downloaded. The problem is only with minor Version. The major version example is: https://BaseURL/sites/SubSite/_api/web/GetFileByServerRelativeUrl('/sites/SubSite/Documents/New Folder/xyz.docx')/versions(512)/$value.

The API runs fine when I replace the major numbers with (version IDs) 1024,1536,2048... and so on. The major Versions in this case are (version label) 1.0,2.0,3.0 and so on.

But for the minor version of the file: (version label) 0.1,0.2,0.3 etc I need to pass the version Ids as 1,2,3 which returns the Response:

400: Bad Request.

My Sample code is as below:

Note: It's SharePoint 2013 and not SharePoint Online. So I don't have to pass AccessToken.

 string resourceUrl = "https://<BaseURL>/sites/<SubSite>/_api/web/GetFileByServerRelativeUrl('/sites/<SubSite>/Documents/New Folder/xyz.docx')/versions(1)/$value";            
        //string resourceUrl = string.Format("{0}_api/web/GetFileByServerRelativeUrl('{1}')/versions({2})/$value", APIsource, DestinationPath + "/" + FileName, FileVersion.ID);
        var wreq = WebRequest.Create(resourceUrl) as HttpWebRequest;
        wreq.Headers.Remove("X-FORMS_BASED_AUTH_ACCEPTED");
        wreq.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        //wreq.Headers.Add("Authorization", "Bearer " + AccessToken);
        wreq.UseDefaultCredentials = true;
        wreq.Method = "GET";
        wreq.Accept = "application/json; odata=verbose";
        wreq.Timeout = 1000000;
        wreq.AllowWriteStreamBuffering = true;
        wreq.ContentLength = 0;
        string result = string.Empty;
        string JsonResult = string.Empty;
        try
        {
            WebResponse wresp = wreq.GetResponse();
            Stream fs = wresp.GetResponseStream() as Stream;
            using (FileStream localfs = File.OpenWrite(LocalTempFolderPath + FileName))
            {
                CopyStream(fs, localfs);
            }
            result = "Download Success";

        }
        catch (Exception ex)
        {
            LogError(ex);
            result = ex.Message;
        }
        return result;
    }

2 Answers 2

0

You could try with CSOM, here is my test code for your reference:

#region MyRegion
            string fileUrl = "http://sp:12001/Shared%20Documents/test.docx";
            Uri fileuri = new Uri(fileUrl);
            Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(fileuri.AbsolutePath);
            context.Load(file);
            context.ExecuteQuery();

            FileVersionCollection versions = file.Versions;
            context.Load(versions);
            context.ExecuteQuery();

            Console.WriteLine(file.Versions.Count);
            //Web _Web = context.Web;
            //context.Load(_Web);
            //context.ExecuteQuery();
            foreach (Microsoft.SharePoint.Client.FileVersion _version in file.Versions)
            {
                Uri filename = new Uri(context.Url + "/" + _version.Url);

                //Microsoft.SharePoint.Client.File _Hisfile = context.Web.GetFileByServerRelativeUrl(filename.AbsolutePath);
                //context.Load(_Hisfile);
                //context.ExecuteQuery();
                //ClientResult<Stream> data = _Hisfile.OpenBinaryStream();
                //Console.WriteLine(data.Value);

                using (var client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(@"contoso\administrator", "pw");
                    //client.UseDefaultCredentials = true;
                    client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                    byte[] stream = client.DownloadData(filename);
                    System.IO.FileStream _FileStream =
                        new System.IO.FileStream(@"C:\Lee\test" + _version .VersionLabel+ ".docx", System.IO.FileMode.Create,
                              System.IO.FileAccess.Write);
                    // Writes a block of bytes to this stream using data from
                    // a byte array.
                    _FileStream.Write(stream, 0, stream.Length);

                    // close file stream
                    _FileStream.Close();
                    Console.WriteLine(stream.Length);
                }

            }
            #endregion

Best Regards, Lee

1
  • Thanks Lee. But I want this in REST API. I'm migrating the files from SP 2013 to SP online through REST API and I have written code for everything from looping through folders and files to create the similar structure in the target site. Only the problem is with one REST api mentioned above. Commented Dec 28, 2016 at 5:57
0

I was able to download the older minor version of the file after a number of tries using different combination of API endpoint. And, the one which worked out for me was:

https://BaseURI/sites/SubSite/_vti_history/1/Documents/New Folder/xyz.docx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.