I'm trying to create a hyperlink that links to a weekly updated document. The Sales document gets updated weekly and because of this the title changes, for example, the naming structure is as follows:

Weekly sales activity 120515.xls

and the following week it'll be

Weekly sales activity 190515.xls

and so on.

I have a hyperlink on my homepage to the document library containing this file, but I was wondering if it's possible use the hyperlink to go directly to the file, taking into account the weekly changes in name. Another way would be to link to the latest file added to the document library, if that's possible instead, ignoring the name.

thanks for any help.

  • can you elaborate on "The Sales document gets updated weekly " ? Is this manual upload or through code? if manual upload, why not change old files name before uploading latest file. You can keep latest file name same every time , so that hyperlink need not change on home page
    – Rohit
    Jul 15, 2015 at 9:10
  • Hi Rohit, thanks for that. It's a manual upload, but the filename has to be different so that people can see which week's figures it relates to. If there's a way of the hyperlink grabbing the latest file that's been uploaded, that'd be ideal - it wouldn't matter what the name was then... Jul 15, 2015 at 9:14

3 Answers 3


Why code?

Documents are in a SharePoint Library so you can create a View which displays the single last document.

If you library is on another site in your Site-Collection you have to use the Content Query Web Part to create that View. Same principle, sort descending, only display one item/document.

  • Content Query is a good option if you don't need to keep link text the same.
    – Vader
    Jul 15, 2015 at 15:00

You can embed simple JS via Script Editor that will calculate link for the latest document. Code may look like:

<div id="document-link"></div>
    var baseUrl = location.protocol + "//" + location.host ;
    var odataUrl = baseUrl + "/sitename/_vti_bin/listdata.svc/Weekly%20Sales%20Activity?$select=Name&$top=1&$orderby=Modified desc";
    var libUrl = baseUrl + "/sitename/Weekly%20Sales%20Activity/";
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", odataUrl, false );
    xmlHttp.send( null );
    var result = JSON.parse(xmlHttp.responseText);
    var link = libUrl + encodeURIComponent(result['d'][0]['Name']);
    document.getElementById('document-link').innerHTML = "<a href=" + link + ">The latest document</a>";
  • Thanks Vader. It seems to be working for the most part. 2 things though - If I try to alter from /Documents to the document library I've called it (Weekly Sales Activity) it doesn't return anything. Not a massive problem as I can move the documents to the /Documents folder if necessary. Main problem - it doesn't seem to handle spaces in the file names - my files are all called 'Weekly Sales Activity 150515' (and other dates), and it only returns the 'Weekly' when clicking the hyperlink. Jul 15, 2015 at 14:23
  • You need to encode library name and document name to get it working. See updated answer.
    – Vader
    Jul 15, 2015 at 14:56

It seems unattainable as hyperlinks are static and the files are constantly changing. The easiest way out is to create a folder before the document and assign the folder hyperlink instead.

For automated and more complicated way, you will need to use code wherein the hyperlink is generated on page load. The link will be based on the latest ModifiedBy value of the list item in the specific folder. (using CAML query)

  • 1
    Or use a javascript onclick event calculating the URL and then sending the user on its way.. Jul 15, 2015 at 9:16
  • That sounds promising Robert. Don't suppose you've any pointers on the code have you? Thanks Jul 15, 2015 at 9:18

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.