SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working with SP.js to get data from a SharePoint library.

Below is the code i am using:

var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle(listName);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View Scope="RecursiveAll"><Query><OrderBy><FieldRef Name="Title" /></OrderBy></Query></View>');
    this.collListItem = oList.getItems(camlQuery);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededLoad), Function.createDelegate(this, this.onQueryFailedLoad));

function onQuerySucceededLoad(sender, args) {
    var olistItemEnumerator = collListItem.getEnumerator();
    var ocount = collListItem.get_count();
    if (ocount > 0) {
        while (olistItemEnumerator.moveNext()) {
            var oListItem = olistItemEnumerator.get_current();
        var data = oListItem.get_data();
        var path = oListItem.get_path();
                otitle: oListItem.get_item('Title'),
                ocapability: oListItem.get_item('Category'),
                ourl: oListItem.get_item('Answer')


I am able to get field values but i am not able to create a URL which will point to the document. I want to create a download link.


share|improve this question
up vote 1 down vote accepted

Please look at the EncodedAbsUrl, FileDirRef, and FileRef properties. The one you use depends on your needs but I suspect you'll want EncodedAbsUrl or FileRef. Here is an example of what each returns:


FileDirRef: /Pages

FileRef: /Pages/home.aspx

var ctx = new SP.ClientContext(siteUrl);
 var items = ctx.get_web().get_lists().getByTitle('Pages').getItems(new SP.CamlQuery());
 ctx.load(items, "Include(Id, Title, Author, EncodedAbsUrl, FileDirRef, FileRef)");
 ctx.executeQueryAsync(function () {
      var listEnumerator = items.getEnumerator();
      while(listEnumerator.moveNext()) {
          var page = listEnumerator.get_current();
          alert(page.get_item('Title') + ' - ' + page.get_item('Author').get_lookupValue() + ' - ' + page.get_item('EncodedAbsUrl') + ' - ' + page.get_item('FileDirRef') + ' - ' + page.get_item('FileRef'));
share|improve this answer

You can get the document link using following line:

var url = _spPageContextInfo.webAbsoluteUrl + oListItem.get_item('FileRef');

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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