0

When trying to execute the following code i got the mentioned error. Here is the snippet:

var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
clientContext.load(web);
clientContext.executeQueryAsync(Get, Failed);

function Get()
    {
       alert(web.get_url());
    }
function Failed()
    {
       alert('Failed' + args.get_message());
    }

I've tried it on root site (/sites/root) and subsite (/sites/root/subsite/), still the same.

Thanks.

UPD

When trying to execute this snippet:

var clientContext = new SP.ClientContext.get_current();
console.log(clientContext)
var web = clientContext.get_web()
console.log(web)
var subsites = web.get_webs();
console.log(subsites)

clientContext.load(subsites);
clientContext.executeQueryAsync(function(s, a)
{   
    var userInfo = [];
    var siteenum = subsites.getEnumerator();
    while (siteenum.moveNext())       
        {
        var oweb = siteenum.get_current();
        var webtitle = oweb.get_title();
        userInfo.push(webtitle);
        }
    console.log(userInfo.join('')); 
}
);

i got the following:

SP.ClientContext {$r_0: Object, $1P_0: "/sites/root", $1O_0: Object, $8_0: SP.ClientRequest, $O_1: SP.Web…} SP.Web {$0_0: SP.ClientContext, $4_0: SP.ClientObjectData} SP.WebCollection {$0_0: SP.ClientContext, $4_0: SP.ClientObjectData}

site1 site2 site3

But when adding

var weburl = oweb.get_url();
userInfo.push(webtitle + weburl);

The error is still the same:

Uncaught TypeError: oweb.get_url is not a function

1 Answer 1

0

Probably the "sp.js" isn't loaded yet or isn't loaded correctly.

Try to use this code**:

var clientContext;
var website;

// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

// Create an instance of the current context and get the website.
function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    website = clientContext.get_web();

    clientContext.load(website);
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
    alert('URL of the website: ' + website.get_url());
}
function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

**The example in this link: https://msdn.microsoft.com/en-us/library/office/jj246877.aspx

1
  • Thanks but already tested this one. Still the same error (Uncaught TypeError: website.get_url is not a function). The following code works: var ctx = new SP.ClientContext(); var site = ctx.get_site(); ctx.load(site); ctx.executeQueryAsync(function(s, a){alert(site.get_url())}); So looks like sp.js is loaded as context can be found.
    – Sergey
    Commented Jul 24, 2015 at 13:13

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.