SharePoint 2010 – Using Document Id to Link to a Specific Version

SharePoint 2010 introduces Document Id, which is an easy way to create document permalinks across a SharePoint Site (SPSite), without worrying about changing names and folders.
This is all nice and well, but what if you’ve enabled versioning, and want to link to a specific version?
It is a shame DocIdRedir.aspx does not accept a version as an optional parameter, but a small shame, as it is easy enough to implement your own handler, using the API method DocumentId.FindUrlById. Alternately, you could have done that yourself and used DocumentId.FindUrlsById, find the appropriate SPListItem and find the version url, if you don’t approve of the way FindUrlById does that for you.
My solution is to create another page, much like DocIdRedir.aspx, and have it accept a version and act accordingly (in this case I’ve created an ASHX handler, but it should be just the same):

public void ProcessRequest(HttpContext context)
	string docId = context.Request.QueryString["id"];
	string versionLabel = context.Request.QueryString["v"];
	SPSite currentSite = SPContext.Current.Site;
	string url = null;
		url = GetUrlFromID(currentSite, docId, versionLabel);
	catch (Exception ex)
		string message = String.Format("Error finding document by id {0} and version [{1}], {2}"
			, docId, versionLabel, ex.Message);

	if (url == null)
		string message = String.Format("Could not find document with id {0} and version [{1}]"
			, docId, versionLabel);

private string GetUrlFromID(SPSite site, string docId, string version)
	//FindUrlById throws an exception if version is empty or null,
	// so I check it here to make sure it works.
	if (String.IsNullOrEmpty(version))
		string[] urls = DocumentId.FindUrlsById(site, docId);
		return urls.Single(); // think about what you do here
		return DocumentId.FindUrlById(site, docId, version);

After creating and deploying your package, which is effortless in Visual Studio 2010, instead of using the default permalinks:


you can use your own handler (you may want a shorter solution name and handler):


See also:


4 thoughts on “SharePoint 2010 – Using Document Id to Link to a Specific Version

    • Thanks Judd.
      I did have trouble with the token replacement on my ASHX file – I hardcoded the strong name (which isn’t that bad, really, but less pretty).
      Do you mean ASHX files are not supported in Visual Studio 2010 for SharePoint projects, or are you suggesting my ASHX might stop working, and I’m not supposed to use it in SharePoint?

  1. Pingback: Quickly looking up documents using Document ID’s in SharePoint – Part 2 « A Content Manifesto

  2. I’ve found this very helpful, and extended the approach further to return a JSON formatted result that includes many different URL’s and properties, including links to document property pages, explicit links to last published version, etc.

    The main idea here is that you often need many pieces of information, and don’t want to perform multiple lookups. Thanks for the post, it was helpful!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.