Question

Originally titled "Get document-version independent URL"

I'm using the Versions.asmx SharePoint webservice, where I get the following output message:

<results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
 <list id="{E0A3AEC1-A35E-47D6-8595-C89E75ECAA06}" />
 <versioning enabled="1" />
 <settings url="http://server/Site/_layouts/LstSetng.aspx?List={E0A3AEC1-A35E-47D6-8595-C89E75ECAA06}" />
 <result version="@2.0" url="http://server/Site/DocLib/Folder/doc.xlsx" created="10/8/2012 1:14 PM" createdRaw="2012-10-08T11:14:46Z" createdBy="SHAREPOINT\system" createdByName="System Account" size="109694" comments="" />
 <result version="1.0" url="http://server/Site/_vti_history/512/DocLib/Folder/doc.xlsx" created="10/8/2012 1:12 PM" createdRaw="2012-10-08T11:12:23Z" createdBy="SHAREPOINT\system" createdByName="System Account" size="110111" comments="" />
</results>

The problem here is that the latest version (marked with the @ symbol) always returns the original URL, but what I really want, is an URL like the other versions (like /_vti_history/<M*512+m>/DocLib/Folder/doc.xlsx) that will serve as a permanent link to the version that is current now for the times when it gets superseded by later versions. Anyone an idea how to get this? (if possible)

Was it helpful?

Solution 2

Since SharePoint doesn't accept the URL to the current version via /_vti_history/ as valid (returning 400 or 404 as HTTP status), we must substitute these standard URLs with links to a custom ASP.NET page which in turn redirects to the correct URL of the version, whether it's current or historical. We pass the versionlabel in the querystring of this new page.

Update: Stefan Stanev has a sample of such page: http://stefan-stanev-sharepoint-blog.blogspot.com/2010/05/how-to-display-all-versions-in.html

The core part of the page in his implementation is as follows:

    string fileRef = this.Request.QueryString["FileRef"];
    int version = int.Parse(this.Request.QueryString["Version"]);

    SPWeb web = SPContext.Current.Web;
    SPFile file = web.GetFile(fileRef);
    SPListItem item = file.Item;

    string redirUrl = null;
    if ((int)item["_UIVersion"] == version) redirUrl = web.Url.TrimEnd('/') + "/" + file.Url;
    else
    {
        foreach (SPListItemVersion v in item.Versions)
        {
            if ((int)v["_UIVersion"] == version)
            {
                redirUrl = web.Url.TrimEnd('/') + "/_vti_history/" + version + "/" + file.Url;
                break;
            }
        }
    }
    if (redirUrl != null)
    {
        Response.Redirect(redirUrl);
    }

Stefan also defines a custom field to display a link for each version

OTHER TIPS

Direct link to a certain version of a document : http://theressomethingaboutsharepoint.blogspot.nl/2011/09/direct-link-to-certain-version-of.html

Update: This is a link to a solution by Hans Worst, who suggests to rely on javascipt to asynchronously check for the existence of the Version's existence.

The core function goes below:

function check_file(path_of_file) 
{
  http_check.open("HEAD", path_of_file);
  http_check.onreadystatechange = handleHttpResponse_check;
  http_check.send(null);

  function handleHttpResponse_check()
  {
    if(http_check.readyState == 4){
        if (http_check.status == 200) {
            window.open(path_of_file);
        }else if (http_check.status >= 400) {
            window.open('/site/Documents/Specifications.pdf');
        }else {
            window.open('/site/Documents/Specifications.pdf');
        }
    }
  }
}

He initially checked for http_check.status == 404, but as the server might return 400 instead, hence the slight change to greater than or equal to 400.

Also, Hans uses a calculated field to display the link to a major version:

Formula: ="<a href='javascript:' onclick="&CHAR(34)&"check_file('/site/_vti_history/"&VALUE(LEFT(DocVersion;1))*512&"/Documents/Specifications.pdf')"&CHAR(34)&">Standard specifications</a>"

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top