0

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>

1 Answer 1

0

The SharePoint JavaScript client object model only works on pages that are a part of the SharePoint framework (i.e. in Web Parts, Application Pages, etc.).

See similar question here

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.