1

I have access to a document library item URL. E.g. /Documents/Forms/DispForm.aspx?ID=585
Based on this information, I would like to get the URL of the document associated with this item. E.g. /Documents/document-name.pdf

All using only javascript.

E.g. Pseudo-code:

function get-document-name(item-url)
{
  return document-url; // E.g. /Documents/document-name.pdf
}

Is this possible?

  • what about using jQuery? Why only JavaScript? – Mike Nov 7 '13 at 20:08
  • Hi Mike, Either way is fine. My restriction is that I cannot use any other script or compiled code. – Tom Nov 7 '13 at 20:16
2

Here's how to get it using jQuery and SharePoint 2010's REST interface so you don't have to write CAML code.

function get-document-name(item-url) {
    var doc-id = item-url.split('=')[1]; ///Documents/Forms/DispForm.aspx?ID=585
    var rest-url = "http://someserver/_vti_bin/listdata.svc/Documents(doc-id)";
    var document-url = "";

    $.getJSON(rest-url,function(data) {
        $.each(data.d.results, function(i,result) {
            var item-path = result.Path.toString();
            var item-name = result.Name.toString();
            document-url = item-path + item-name;
        });
    }); 
    return document-url; // E.g. /Documents/document-name.pdf
}    
  • Hello Craig, Thank you for posting this. I have been playing around with your code; but I am not sure about where to drop this script in the body. I chose to drop it in the very last content place holder on the list view page, but the function is returning empty string; possibly because, the page data is not being loaded before the script executes. Please let me know what I need to do to get this working as intended. Thank you. – Tom Nov 12 '13 at 19:15
  • On second thought, should the page load even matter? At the moment, I am hard-coding the doc-id just to get some kind of result. Its not happening for some reason. – Tom Nov 12 '13 at 20:00
  • You might want to use the F12 tools add some console.log(''); lines in for showing that the splitting and lookups are working right. function get-document-name(item-url) { var doc-id = item-url.split('=')[1]; ///Documents/Forms/DispForm.aspx?ID=585 var rest-url = "someserver/_vti_bin/listdata.svc/Documents(doc-id)"; var document-url = ""; console.log('doc-id: ' + doc-id); console.log('rest-url: ' + rest-url); $.getJSON(rest-url,function(data) { $.each(data.d.results, function(i,result) { var item-path = result.Path.toString(); console.log('item-path: ' + item-path) – Craig Pilkenton Nov 12 '13 at 21:23
  • Hi Craig, when I paste the restUrl (with static ID) to the browser address bar, I get the xml file with results. But, when I run the same code in the page, ***$.getJSON(restUrl,function(data) { console.log(data.d.results); ... *** is undefined. – Tom Nov 12 '13 at 22:10
  • 1
    Interesting. I've been using 1.8.x and above for more functionality. If it's erroring so much you may want to swap out $.getJSON with $.ajax which has error handling. asp.net/ajaxlibrary/jquery_errors.ashx – Craig Pilkenton Nov 13 '13 at 14:20
1

I'm not sure what format you want the URL, as in where, but this adds the URL to the page in a tag. You need to download SPServices and jQuery and host them in your SharePoint library, such as 'Style Library' or a document library.

<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="filelink/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">

var myQuery = "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + ID + "</Value></Eq></Where></Query>";

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements", //YOUR LIST NAME HERE
    CAMLQuery: myQuery,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("FileLeafRef") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>
  • Hi Mike, Thank you for the spot-on response. My specific issues is this: I am trying to display the URL to the document in the calendar view of the document library. Currently, it displays the link to the items. So, when the user clicks on any item shown on the calendar view, it shows the item view pop-up. Based on your response, I am going to have to make a list of all items displayed in the calendar view. Then, apply the function you provided for each item ID. This should be fun :) Unless, you have a better suggestion for me. Thank you again! – Tom Nov 7 '13 at 21:04
0

If you are not fond of programming, I found a rather simple way to get the url to a given item in a given list using right click on the 'edit item (link to edit entry)' column that you can add to any view.

I then trained my users to right click on this and use Ctrl-K in any office document to create a neat pointer directly to the item in the list.

The URL looks like :

http://intranet.company.local/units/subsite/sandbox/_layouts/listform.aspx?PageType=6&ListId={8B3C354B-392A-4B35-BFBB-C548CA339CEF}&ID=1

Note: because it is using list ID and item ID it should resist list renaming and moving within the given site collection .

I am also convinced that the PageType parameter could be used to display another form, (view instead of edit) and that other parameters also exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.