0

I have SharePoint site, wherever I open developer tools and type: SP. I don't get the ClientContext as a suggestion, it's not loaded at all in SharePoint. I tried that on SharePoint application pages, on site pages, on different master pages. Any idea what might be the reason?

3 Answers 3

1

SP.Js is not included in the master page. Since SP.SOD.executeFunc supports on demand scripts, there is no need to reference SP JavaScript files using SharePoint:ScriptLink in master pages. Just use the following code and it should show the title of the web. You can run this code in dev tools code window:

function readWebTitle() {   
    var clientContext = new SP.ClientContext.get_current();
    this.web = clientContext.get_web();     
    clientContext.load(this.web, 'Title');
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    console.log(this.web.get_title())   
}

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

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', readWebTitle);

See the answer by Vadim Gremyachev Are the SP.js and SP.UserProfiles.js preloaded in SharePoint?

11
  • Thank you Nadeem, but if you try to create a team site, and open dev tools, type SP. you'll see ClientContext suggested, but that doesn't happen on my website, which leads me to think that ClientContext will be populated there whether you ask for it or not, which happens in the newly team site you would create. Feb 11, 2015 at 11:21
  • Second thing, I am using sp.sod.executeordelayuntilscriptloaded but it's not working too. Is it different than SP.SOD.executeFunc? Feb 11, 2015 at 11:22
  • But does the above code work? Feb 11, 2015 at 11:31
  • Yes, but I don't get it.. Feb 11, 2015 at 11:45
  • 1
    Good question, let me do some research about this. Feb 11, 2015 at 11:58
0

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.
function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    website = clientContext.get_web();

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

Check this msdn url.

6
  • Thanks for your suggestion. The ClientContext is not even loaded after the whole page loads. As said in my question, even after the page loads and I open dev tools (F12). ClientContext is not loaded when I type: SP... Feb 11, 2015 at 10:41
  • What version of SharePoint and which browser are you using. I have noticed the IE developer tools don't work in SP 2010. However, to works with chrome and SP 2010 and IE and SP 2013 Feb 11, 2015 at 10:49
  • I am also using SharePoint Seattle master page Feb 11, 2015 at 10:50
  • An Update- I verified it's working on Seattle.master, but not working on my custom master page, what would be the issue? Feb 11, 2015 at 10:59
  • 1
    I have checked the seattle.master, even sp.js is not loaded in there, only these are loaded: core.js, menu.js, callout.js, sharing.js, suitelinks.js Feb 11, 2015 at 11:06
0

Try to load 'sp.js' file on back-end code like this:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write(BindScript("~site/Style Library/script/sp.js", true));
            writer.Write(BindScript("~site/Style Library/script/sp.core.js", true));
            writer.Write(BindScript("~site/Style Library/script/SP.Runtime.js", true));          
            base.Render(writer);
        }

private string BindScript(string scriptUrl, bool PickFromSiteCollection)
        {
            if (PickFromSiteCollection)
                scriptUrl = Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(SPContext.Current.Site.RootWeb.Url, scriptUrl);
            else
                scriptUrl = Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, scriptUrl);

            return string.Format(@"<script type=""text/javascript"" src=""{0}""></script>", scriptUrl);
        }
1
  • Hi Ronak, thanks for the suggestion. I already have the SP object in my browser, but it has limited set of properties and methods, the ClientContext is not one of them. Feb 11, 2015 at 10:55

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.