1

I have a custom html and javascript file housed within a document library of a subsite: http://teamsite/project/testing%20site/shared%20documents/test.html and

http://teamsite/project/testing%20site/shared%20documents/test.js




function testContext(){
var clientContext = new SP.ClientContext("http://teamsite/projects/testing%20site/");
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();

clientContext.load(collList);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onWin), Function.createDelegate(this, this.onFail));

function onWin(){alert("Success");}
function onFail(){ alert("Fail");}

Everytime I execute the above I get this error:

SCRIPT5022: Sys.ArgumentException: Value does not fall within the expected range. Parameter name: serverRelativeUrl sp.runtime.js, line 2 character 38258

I've tried several different methods to get CSOM to work in this instance and everytime I get the same error.

2 Answers 2

3

The constructor SP.ClientContext(serverRelativeUrlOrFullUrl) expects serverRelativeUrlOrFullUrl to be the server-relative URL that:

  • starts with /
  • or a full URL that starts with http:// or https://

in SharePoint 2013.

In SharePoint 2010 it could be only the server-relative URL that starts with /.


As an alternative you could utilize:

var ctx = SP.ClientContext.get_current();

to get the current client context of the client-side object model (CSOM) runtime

2
  • This doesn't appear to work because the javascript lives outside of webparts. Code: var ctx = SP.ClientContext.get_current(); var oWebsite = ctx.get_web(); this.collList = oWebsite.get_lists(); clientContext.load(collList); clientContext.executeQueryAsync(Function.createDelegate(this, this.onWin), Function.createDelegate(this, this.onFail)); Throws 2x errors: SCRIPT5007: Unable to get value of the property 'webServerRelativeUrl': object is null or undefined SCRIPT5022: Sys.ArgumentException: Value does not fall within the expected range. Parameter name: serverRelativeUrl Commented Sep 26, 2014 at 14:43
  • So assuming the HTML and Javascript are executing out of site/sitename/shareddocuments and the list I want to access is in site/sitename I have attempted absolute URL reference 'site/sitename' (fails) and the relative URLs '../' and 'site/sitename' and '../.' and all fail. Commented Sep 26, 2014 at 14:48
1

Use like this:

var siteUrl = '/sites/MySiteCollection';

var clientContext = new SP.ClientContext(siteUrl);

Use relative Url in site url

http://msdn.microsoft.com/en-us/library/office/hh185009(v=office.14).aspx

6
  • Per my example I assume you mean: siteUrl = 'projects/testing%20site' which returns the same error. Commented Sep 26, 2014 at 12:46
  • As in example start your url with "/"
    – Aanchal
    Commented Sep 26, 2014 at 14:15
  • It appears to have executed but now throws the error: "Request failed. The security validation for this page is invalid and might be corrupted. Please use your browser's Back button to try your operation again. undefined." Commented Sep 26, 2014 at 14:56
  • Are you calling this from another site collection or from same site collection.
    – Aanchal
    Commented Sep 26, 2014 at 15:00
  • If you turn off the Web Page Security Validation, the security goes away. Manage Web Application----> Select the web application---Click on General Settings(under the ribbon) and turn off the Web Page Security Validation.
    – Aanchal
    Commented Sep 26, 2014 at 15:02

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.