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?
Asked
Active
Viewed 1,981 times
3 Answers
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
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
-
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... – Brittany Rutherford Feb 11 '15 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 – Nadeem Yousuf-AIS Feb 11 '15 at 10:49
-
-
An Update- I verified it's working on Seattle.master, but not working on my custom master page, what would be the issue? – Brittany Rutherford Feb 11 '15 at 10:59
-
How do I check that? Isn't it included by default in master pages? I can see the SP object in dev tools, but can't see the ClientContext :S – Brittany Rutherford Feb 11 '15 at 11:02
-
1I 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 – Brittany Rutherford Feb 11 '15 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);
}
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
sp.sod.executeordelayuntilscriptloadedbut it's not working too. Is it different than SP.SOD.executeFunc? – Brittany Rutherford Feb 11 '15 at 11:22SP.SOD.executeFuncloads 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.executeordelayuntilscriptloadedneeds 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