3

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();

    clientContext.load(this.oWebsite);

    clientContext.executeQueryAsync(
        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.

EDIT:

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) {
        $this.find('li').each(function(i){
            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().');
                retrieveWebSite(siteUrl);
            }
        });
    }
}  
3
  • What is the value of oWebsite ?
    – Perplexed
    Commented Nov 28, 2013 at 14:05
  • I am having same issue. Did you get any solution?
    – variable
    Commented Mar 25, 2014 at 6:35
  • Did you found any solution to above ?? Please let me know if you have any solution. Thanks. -Pethuraj.
    – user51932
    Commented Mar 3, 2016 at 10:33

1 Answer 1

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();
2
  • I'll add code to my original question to explain how I fetch the value of the siteUrl.
    – Magali
    Commented Nov 29, 2013 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
    Commented Nov 29, 2013 at 8:53

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.