1

I am busy writing some javascript to take a list of selected documents and do some processing on a list with each document in Office365. I am getting a strange error when the code tries to load the document from the absolute URL. The document is guaranteed to be in the same SP.Web that the JS is in.

The error is

Value does not fall within the expected range.

Below is the JS I have:

function GetFile(attachment, item) {
   var fileContext = SP.ClientContext.get_current();

   var site = fileContext.get_site();
   fileContext.load(site);

   fileContext.executeQueryAsync(function() {
       var relUrl = attachment.Url.replace(_spPageContextInfo.siteAbsoluteUrl, '');

       var file = fileContext.get_web().getFileByServerRelativeUrl(relUrl);

       fileContext.load(file, 'ListItemAllFields');

       fileContext.executeQueryAsync(
           function () {
               var listItem = file.get_listItemAllFields(); 
           },
           function (sender, args) {
               console.log(args.get_message()); //errorhandling
           }
       );
   }, function(sender, args) {
       console.log(args.get_message());
   });  
}

This is not a permissions thing as I can take the url of the documents being selected and open them fine from the browser.

I have noticed, that I can't seem to hit the client.svc service on the site in question when taking the url for the request from the request object.

1
  • What is attachment parameter? Commented Oct 10, 2014 at 13:07

2 Answers 2

1

The error Value does not fall within the expected range usually occurs when the file specified via serverRelativeUrl parameter in SP.Web.getFileByServerRelativeUrl Method does not exists, so make sure the url has the following format:

/<site>/<list url>/Attachments/<item id>/<attachment name>

I guess (since it is not clear from question) attachment.Url stores the location of AttachmentFile. In that case SP.File.listItemAllFields property will be empty since attachment file is not associated with list item.

2
  • The naming seems to be miss-leading here. Attachment.Url is a URL to a file stored in a library within the same site, these are not attachments in a list. What I am looking to do is create a new list item and attached the selected files to the item.
    – MrThirsty
    Commented Oct 13, 2014 at 6:35
  • I would recommend you in future to specify a complete code examples in order to avoid this type of miss-leading, because from your question we could only guess what attachment variable means.. Commented Oct 14, 2014 at 18:07
0

You suggestion of the "file doesn't exist" led me down the right road. The url I was using to grab the file was kissings /sites/ which was getting removed by the following:

var relUrl = attachment.Url.replace(_spPageContextInfo.siteAbsoluteUrl, '');

I have made some changes to it and it now loads the file as expected.

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.