Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I used the code from "How to: Complete basic operations using JavaScript library code in SharePoint 2013", the part underneath "SharePoint website tasks".

I'll just paste the code here, so that you can see it without having to open the above site:

 function retrieveWebSite(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();


        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() + 
        ' Description: ' + this.oWebsite.get_description());

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\nStackTrace: ' + args.get_stackTrace());

Whenever I run it, I get the following error:

Request failed. Unexpected response from server. StackTrace: null

I am absolutely clueless on why it won't work. I even tried adding the code below at the top of the script (which I collected from browsing other questions here), in the hopes that it would help. But alas. Even that won't do it.

SP.SOD.executeOrDelayUntilScriptLoaded('SP.UserProfiles.js', "~sitecollection/Style%20Library/Scripts/jquery.SPServices-2013.01.min.js");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');
$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(retrieveWebSite, "sp.js"); });

Any help would be greatly appreciated.


This is the code I use to loop through all elements in the quick launch navigation (it is a term driven navigation), as soon as it encounters an element that has a href attribute ending with "Virtual-desk.aspx", it launches the function retrieveWebsiteUrl(). So basically with this code I give a value to the siteUrl variable. The function runMe() is called as soon as the document is ready.

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(runMe, "sp.js"); });

var siteUrl = '';
var counter = 0;
var clientContext;

function runMe() {
    var $this = $("#NavRootAspMenu"); 
    if($this != null) {
            counter = counter + 1;  
            siteUrl = $this.find("a.static")[i].href;
            console.log('List item found, count = ' + counter + '; ---------- ' + siteUrl); 
            if (siteUrl.indexOf('Virtual-desk.aspx') > -1) {
                console.log('Encountered the page "Virtual-desk.aspx". Now running retrieveWebSite().');
share|improve this question
What is the value of oWebsite ? –  Perplexed Nov 28 '13 at 14:05
I am having same issue. Did you get any solution? –  variable Mar 25 at 6:35

1 Answer 1

What is the value of the siteUrl variable? Ideally it should be a url to the site you are trying to open.

Or if you want to open the current site, you can do:

var clientContext = SP.ClientContext.get_current();
share|improve this answer
I'll add code to my original question to explain how I fetch the value of the siteUrl. –  Magali Nov 29 '13 at 8:42
If I use SP.ClientContext.get_current() instead of SP.ClientContext(siteUrl), it will only return the url of the page the user is currently on. I need it to work with 'SP.ClientContext(siteUrl)` because I'm looping through a list of urls. –  Magali Nov 29 '13 at 8:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.