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?

Brittany Rutherford
  • 878
  • 2
  • 16
  • 35

3 Answers3

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?

Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
  • 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. – Brittany Rutherford Feb 11 '15 at 11:21
  • Second thing, I am using sp.sod.executeordelayuntilscriptloaded but it's not working too. Is it different than SP.SOD.executeFunc? – Brittany Rutherford Feb 11 '15 at 11:22
  • But does the above code work? – Nadeem Yousuf-AIS Feb 11 '15 at 11:31
  • Yes, but I don't get it.. – Brittany Rutherford Feb 11 '15 at 11:45
  • There is a concept of loading scripts on demand in SharePoint. That is a script is not loaded unless it is explicitly called for. SP.SOD.executeFunc loads the script in this case sp.js even if it is defined no where and that is why you don't see it in dev tools. However, sp.sod.executeordelayuntilscriptloaded needs the script to be present in order to work. It can wait for the script if it hasn't been loaded yet. But script reference needs to be ther. – Nadeem Yousuf-AIS Feb 11 '15 at 11:51
  • But why when I create a team site, and try to verify if SP.ClientContext is there, I can get it without even needing to to SP.SOD.executeFunc? That's what confusing me – Brittany Rutherford Feb 11 '15 at 11:53
  • 1
    Good question, let me do some research about this. – Nadeem Yousuf-AIS Feb 11 '15 at 11:58
  • @BrittanyRutherford, what out of the box .js files are included in your master page? – Nadeem Yousuf-AIS Feb 11 '15 at 13:31
  • hi Nadeem, these are the files included by default in the master page: core.js, menu.js, callout.js, sharing.js, suitelinks.js – Brittany Rutherford Feb 11 '15 at 13:38
  • Yes these are in seattle.master. What about the custom master page using which you don't get the context. – Nadeem Yousuf-AIS Feb 11 '15 at 13:41
  • same references, I am also referencing a custom js file, but that's it. – Brittany Rutherford Feb 11 '15 at 16:34
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.

Kosikovec
  • 194
  • 9
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);
        }
Ronak Patel
  • 3,261
  • 3
  • 23
  • 45
  • 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. – Brittany Rutherford Feb 11 '15 at 10:55