Viewing a specific file version

561 views Asked by At

Using an ithit-webdav server implementation in combination with an ASP.NET MVC4 application I represent several Excel documents to the user that can be opened and saved to the webdav server including versioning. Editing is done by a JavaScript function that will handle a click on the file's URL that is on the page. For IE the handler uses the Sharepoint plugin and otherwise the winFirefoxPlugin. This all works fine. The user can check-out a file, edit and save the file, check-in the file and the version number is increased. But now the user should be able to show a previous version of the file. I can show a list of all existing versions of a file but I don't know how I can generate link that the user can click for opening the file using the javascript handler. The only thing I can think of is creating a temporary file containing the specific version on the webdav server and open the temporary file (when to delete?).
Does someone know a better solution?

var editDocument = function () {
  var $a = $(this),
     doc = $a.attr("href");
  var plugin = navigator.plugins["SharePoint.OpenDocuments"];
  if (!plugin) {
    try {
      plugin = new ActiveXObject('SharePoint.OpenDocuments');
    } catch (err) {
      //window.alert("Unable to create ActiveX object");
      //return false;
    }
  }
  if (plugin) {
    plugin.EditDocument(doc);
  } else {
    try {
      var ffPlugin = document.getElementById("winFirefoxPlugin");
      ffPlugin.EditDocument(doc);
    } catch (e) {
      alert("Editing not supported. (" + e.message + ")");
    }
  }
  return false;
};

1

There are 1 answers

2
IT Hit WebDAV On BEST ANSWER

The versions URLs totally depend on the server implementation. The code generated by WebDAV wizards for Visual Studio forms version urls like this:

http://server/path/file.ext?version=1

To get file versions list for particular file on the client side in JavaScript you can use the IT Hit WebDAV Ajax Library. The File.GetVersionsAsync() method returns an array of Version objects. Each Version object has a Href property wich is a version URL:

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
webDavSession.OpenFileAsync('http://server/path/file.ext', function(oAsyncResultFile) {

        var oFile = oAsyncResultFile.Result;
        oFile.GetVersionsAsync(function(oAsyncResultVersions) {

                var aVersions = oAsyncResultVersions.Result;
                for(var i=0; i<aVersions.length; i++)
                {
                     console.log(aVersions[i].Href);
                }

            });
    });

In case you wish to get the versions list request yourself here is an example of request and response:

REPORT http://localhost:7865/DAV/qtest_1417713485047/Versions/ver.txt
Content-Type: text/xml; charset="utf-8"
<?xml version="1.0"?><version-tree xmlns="DAV:"><prop><displayname/><creationdate/><getcontenttype/><getcontentlength/><version-name/><creator-displayname/><comment/></prop></version-tree>

207 Multi-Status
X-Engine: IT Hit WebDAV Server .Net v3.9.2075.0 (Evaluation License)
Date: Thu, 04 Dec 2014 17:19:10 GMT
Content-Type: application/xml; charset=utf-8
Content-Length: 667
<?xml version="1.0" encoding="utf-8"?>
<d:multistatus xmlns:d="DAV:">
  <d:response>
    <d:href>http://localhost:7865/DAV/qtest_1417713485047/Versions/ver.txt?version=1</d:href>
    <d:propstat>
      <d:status>HTTP/1.1 200 OK</d:status>
      <d:prop>
        <d:displayname>ver.txt</d:displayname>
        <d:creationdate>2014-12-04T17:19:10Z</d:creationdate>
        <d:getcontenttype>text/plain;charset=UTF-8</d:getcontenttype>
        <d:getcontentlength>6</d:getcontentlength>
        <d:version-name>V1</d:version-name>
        <d:creator-displayname />
        <d:comment />
      </d:prop>
    </d:propstat>
  </d:response>
</d:multistatus>

IMPORTANT! The code generated by WebDAV wizards for Visual Studio generate version URLs with parameters. Microsoft Office truncates parameters when opening documents. That's why you must change your server implementation to generate version urs without parameters if you want to save versions back to server. The version URL must look for example like this:

http://server/path/file.ext/<v1>

The code that generates the version URL is located in Version.cs file:

    public static string CreateVersionPath(string itemPath, int versionNumber)
    {
        return itemPath + "?version=" + versionNumber;
    }

The code that parses the version url and returns Version object to the engine is in DavContext.cs file:

    public override IHierarchyItem GetHierarchyItem(string path)
    {
        path = path.Trim(new[] { ' ', '/' });
        if (path.Contains("?version="))
        {
            return readVersion(path);
        }
        …
    }