未捕获的TypeError:SP.ClientContext不是构造函数 [英] Uncaught TypeError: SP.ClientContext is not a constructor

查看:76
本文介绍了未捕获的TypeError:SP.ClientContext不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从SharePoint在线列表中获取列表项。但是,我收到一个错误说明:"Uncaught TypeError:SP.ClientContext不是构造函数" at line:var context = new SP.ClientContext(siteUrl); 

我已经尝试了很多但没有得到解决方案。请有人告诉我这个问题的解决方法是什么?
$

Hi, I am trying to fetch list items from my list in SharePoint online. But, I am getting a single error stating: "Uncaught TypeError: SP.ClientContext is not a constructor" at line: var context = new SP.ClientContext(siteUrl); 
I have tried a lot but didn't get resolution to issue. Please someone tell me what is the workaround for this problem?

   function retriveListItem()  
    {      
        	    var siteUrl = '/sites/NewWorkSite';
        	    var context = new SP.ClientContext(siteUrl);  
        	    var list = context.get_web().get_lists().getByTitle('Questions1List');  
        	    var caml = new SP.CamlQuery();  
        	    caml.set_viewXml("<View><Query><OrderBy><FieldRef Name='QuestionNumber' Ascending='TRUE' /></OrderBy></Query></View>");  
        	    returnedItems = list.getItems(caml);  
        	    context.load(returnedItems);  
        	    context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
        }  
          
        // Make sure the SharePoint script file 'sp.js' is loaded before your
        // code runs.
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retriveListItem);
        
    function onSucceededCallback(sender, args)  
    {  
        	    var enumerator = returnedItems.getEnumerator();  
        	    //Formulate HTML from the list items   
        	    var MainResult = 'Items in the Divisions list: <br><br>';  
        	    //Loop through all the items   
        	    while (enumerator.moveNext())  
            	{  
        	                     
                             
                    var listItem = enumerator.get_current();  
        	        var qNo= listItem.get_item("QuestionNumber");   
        	        var qst = listItem.get_item("Question");   
        	        var optionA = listItem.get_item("OptionA");	        
        	        var optionB = listItem.get_item("OptionB");
        	        var optionC = listItem.get_item("OptionC");
        	        var optionD = listItem.get_item("OptionD");
        
        	        
        	        	        	                           
                            $('#qDiv').append("<label>" + qNo+ "." + "</label>&nbsp" + "<label id='qts'>" + qst + "</label>" + "</br>" 
                            + "<label>A: </label>&nbsp" + "<input id='one' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'A'/>&nbsp" + optionA  + "&nbsp</br>"
                            + "<label>B: </label>&nbsp" + "<input id='two' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'B'/>&nbsp" + optionB + "</br>"
                            + "<label>C: </label>&nbsp" + "<input id='three' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'C'/>&nbsp" + optionC + "</br>"
                            + "<label>D: </label>&nbsp" + "<input id='four' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'D'/>&nbsp" + optionD + "</br>" + "</br>");
                            
                     
        		 
                }  
                
                                         
        }  
                    //This function fires when the query fails   
    function onFailedCallback(sender, args)  
    {  
                    //Formulate HTML to display details of the error   
                    var markup = '<p>The request failed: <br>';  
                    markup1 = 'Message: ' + args.get_message() + '<br>';  
                    //Display the details   
                    alert(markup1);
    } 
          
    retriveListItem();







推荐答案

您好Purushottam,

Hi Purushottam,

我看到您在上面的代码片段中调用了两次retrieveListItem函数。第一个使用SP.SOD.executeFunc,第二个直接在代码的结尾调用函数。

I saw you are calling the retrieveListItem function twice in the code snippet above. First one in using SP.SOD.executeFunc, second one call the function directly in the ending of the code.

这不需要重复调​​用函数和JSOM代码需要调用SP.SOD.executeFunc或者ExecuteOrDelayUntilScriptLoaded将确保在页面中加载sp.js后调用的函数,因此请编辑下面的代码片段:

This is not need to repeat call the function and JSOM code need to call with SP.SOD.executeFunc or ExecuteOrDelayUntilScriptLoaded which will make sure the function called after sp.js loaded in the page, so edit the code snippet below:

    <script type="text/javascript">
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retriveListItem);
	
	function retriveListItem()  
    {      
        	    var siteUrl = '/sites/dev';
        	    var context = new SP.ClientContext(siteUrl);  
        	    var list = context.get_web().get_lists().getByTitle('Questions1List');  
        	    var caml = new SP.CamlQuery();  
        	    caml.set_viewXml("<View><Query><OrderBy><FieldRef Name='QuestionNumber' Ascending='TRUE' /></OrderBy></Query></View>");  
        	    returnedItems = list.getItems(caml);  
        	    context.load(returnedItems);  
        	    context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
    }  
          
   
        
    function onSucceededCallback(sender, args)  
    {  
        	    var enumerator = returnedItems.getEnumerator();  
        	    //Formulate HTML from the list items   
        	    var MainResult = 'Items in the Divisions list: <br><br>';  
        	    //Loop through all the items   
        	    while (enumerator.moveNext())  
            	{  
        	                     
                             
                    var listItem = enumerator.get_current();  
        	        var qNo= listItem.get_item("QuestionNumber");   
        	        var qst = listItem.get_item("Question");   
        	        var optionA = listItem.get_item("OptionA");	        
        	        var optionB = listItem.get_item("OptionB");
        	        var optionC = listItem.get_item("OptionC");
        	        var optionD = listItem.get_item("OptionD");


('#qDiv')。append("< label>" + qNo +"。 " +"< / label>& nbsp" +"< label id ='qts'>" + qst +"< / label>" +"< / br>" ;
+"< label> A:< / label>& nbsp" +"< input id ='one'class ='rdBtn'type ='radio'name ='" + qNo +"'value ='A'/>& nbsp" + optionA +"& nbsp< / br>"
+"< label> B:< / label>&  " +"< input id ='two'class ='rdBtn'type ='radio'name ='" + qNo +"'value ='B'/>& nbsp" + optionB +" < / br>"
+"< label> C:< / label>& nbsp" +"< input id ='three'class ='rdBtn'type ='radio'南e ='" + qNo +"'value ='C'/>& nbsp" + optionC +"< / br>"
+"< label> D:< / label>& nbsp" +"< input id ='four'class ='rdBtn'type ='radio'name ='" + qNo +"'value ='D'/>& nbsp" + optionD +"< / br>" +"< / br>");



}


}
//当查询失败时,此函数将触发
function onFailedCallback( sender,args)
{
//制定HTML以显示错误的详细信息
var markup ='< p>请求失败:< br>';
markup1 ='消息:'+ args.get_message()+'< br>';
//显示详细信息
alert(markup1);
}
< / script>
('#qDiv').append("<label>" + qNo+ "." + "</label>&nbsp" + "<label id='qts'>" + qst + "</label>" + "</br>" + "<label>A: </label>&nbsp" + "<input id='one' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'A'/>&nbsp" + optionA + "&nbsp</br>" + "<label>B: </label>&nbsp" + "<input id='two' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'B'/>&nbsp" + optionB + "</br>" + "<label>C: </label>&nbsp" + "<input id='three' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'C'/>&nbsp" + optionC + "</br>" + "<label>D: </label>&nbsp" + "<input id='four' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'D'/>&nbsp" + optionD + "</br>" + "</br>"); } } //This function fires when the query fails function onFailedCallback(sender, args) { //Formulate HTML to display details of the error var markup = '<p>The request failed: <br>'; markup1 = 'Message: ' + args.get_message() + '<br>'; //Display the details alert(markup1); } </script>

如果直接在代码的末尾调用函数,则会抛出"SP.ClientContext不是构造函数"。错误。

If call the function directly like in the end of the code, will throw "SP.ClientContext is not a constructor" error.

谢谢

最好的问候


这篇关于未捕获的TypeError:SP.ClientContext不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆