SharePoint – Creating dynamic or static document URL

Links are meant to be broken, at least if you move files or change file names of documents in SharePoint.

I had a customer that required links not to be broken when file name was changed or a document was moved. They also wanted to be able to create links to a specific version, current version and latest major version. The idea was by using the document context menu, users would have the option to create these URL’s.

I wrote a post about creating URL handler for document URLs, this time we will focus on how we can reuse that URL handler to create dynamic and static URLs to documents and document-versions directly from the GUI.

Microsoft has implemented a great way to reach documents by the document id, but it seems that they didn’t fully implement it’s potential into SharePoint. Therefor I would like to show you how we implemented a useful way to reach document versions by using the document id.

This implementation requires the document id feature in SharePoint 2010 enabled. It can be found under Site Collection Features.

Creating the context menu feature

Adding a new option to the context menu is quite easy, it’s all XML. Just add a new Module to your project, I called mine CustomContextMenuActions. Then, by editing the Elements.xml, add a new CustomAction like this. By using the query-string IsDlg=1 we tell SharePoint to remove stuff from the master-page that isn’t relevant to the dialog.

It would create an action like this.

contextmenu

Before we can use this, we have to create the application page called createurl.aspx and the JavaScript function openCreateUrl.

Creating the application page

The action redirect will send us to an application page called createurl.aspx. This page will contain the layout and logic that is needed for the creation of the URLs.

First we have to add an application page in our solution. I placed it in /_layouts/customapplicationpages/ and named it createurl.apx.

We want the page to be shown as a ModalDialog. The reason we use a ModalDialog is because it looks clean and we don’t need to navigate away from the current view.

This it what the ModalDialog would look like

copyurlmodal

Markup

Here is an example of the markup for the application page, with inline styling. You can of course move this into your CSS.

Code Behind

This will add the File-name to DocLabel and populate VersionsDropDown with version as text and the URL as value. The extension method TryGetDocumentId can be found in this post.

JavaScript

This JavaScript requires jQuery. I decided to handle rest of the GUI logic in JavaScript, no more code-behind. This JavaScript includes code for the ModalDialog and parent window.

This will create a quite handy feature that will generate the URL’s and present them to the user in a way that is easy to understand. This feature was very appreciated by my customer, they use it to build baselines and add hyperlinks in TFS.

About Tobias Eriksson