I created a html file and paste it under layout folder and browse using the url http://[server-url]/_layouts/sptest.html
. In that file I am just using a simple button. And onclick just return the number of item in pages library but it throw error SP.PageContextInfo.get_$g_0().webServerRelativeUrl
is null or not an object in SP.js file.
Am I missing any thing? Please help!
Here is a code for that file:
<html>
<head></head>
<body>
<input type="button" onclick="copyText()" value="Display input type" id="button1">
<script src="http://win-csmrhq8pr58/_layouts/1033/init.js"></script>
<script src="http://win-csmrhq8pr58/_layouts/MicrosoftAjax.js"></script>
<script src="http://win-csmrhq8pr58/_layouts/sp.core.js"></script>
<script src="http://win-csmrhq8pr58/_layouts/sp.runtime.js"></script>
<script src="http://win-csmrhq8pr58/_layouts/sp.js"></script>
<script type="text/javascript">
function copyText()
{
MainFunction();
//ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js");
}
var objContext = null;
var objWeb = null
var objList = null;
var objItem = null;
function MainFunction() {
objContext = new SP.ClientContext.get_current();
objWeb = objContext.get_web();
objList = objWeb.get_lists().getByTitle("Documents");
objContext.load(objList);
objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess),Function.createDelegate(this,
this.onFail));
}
function onSuccess(sender, args) {
alert('Item Count: ' + objList.get_itemCount());
}
function onFail(sender, args) {
alert('Some error has occured.');
}
</script>
</body>
</html>