1

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.load(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededLoad), Function.createDelegate(this, this.onQueryFailedLoad));

function onQuerySucceededLoad(sender, args) {
    //alert("success");
    var olistItemEnumerator = collListItem.getEnumerator();
    var ocount = collListItem.get_count();
    //alert(web.get_serverRelativeUrl());
    if (ocount > 0) {
        while (olistItemEnumerator.moveNext()) {
            var oListItem = olistItemEnumerator.get_current();
        var data = oListItem.get_data();
        var path = oListItem.get_path();
            otable.push({
                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.

Regards.

2 Answers 2

4

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:

EncodedAbsUrl: http://yourserver.com/Pages/home.aspx

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'));
      }
 });
2

You can get the document link using following line:

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.