When I try to get sub sites of specific sub site

var clientContext = SP.ClientContext('/groupcompanies');
var webs = clientContext.get_web().get_webs();

I get this error:

undefined is not a function >>>>> sp.js


var clientContext = SP.ClientContext.get_current();

works fine, there is always a problem when I define a url for ClientContext


This line

var clientContext = SP.ClientContext('/groupcompanies');

is incorrect, you need to create a new context, like this:

var clientContext = new SP.ClientContext('/groupcompanies');

(Notice the new keyword). Also make sure that /groupcompanies is a valid server relative path to a web.

  • It will be nice if u explain why SP.ClientContext.get_current() works without new keyword ? – Ahmed Aborady Feb 8 '15 at 17:21
  • Mainly because that is a contextual variable to have with you all the time (compare to the CurrentContext in server side code). When you want to create a context for another site you do not have that context and hence need to create a new object for that context. – Robert Lindgren Feb 8 '15 at 18:47

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.