【问题标题】:SharePoint Online Uncaught TypeError: Cannot read property 'apply' of undefinedSharePoint Online 未捕获类型错误:无法读取未定义的属性“应用”
【发布时间】:2019-10-09 17:13:54
【问题描述】:

我正在尝试在用户单击按钮时创建 SharePoint 列表项。 我不知道如何解决我遇到的错误。不过,代码似乎仍能按预期工作。

HTML 源代码:

<button type="button" class="btn btn-info" id="getPollval">Submit</button>'

脚本 - 从单选按钮中获取值:

var pName = '[name="Poll' + pollId + '"]:checked';
var pId = 'Poll' + pollId;
document.getElementById("getPollval").onclick = function() {
    var pollValue = document.querySelector(pName);
    createListItem(pollValue.value, pId , pollTitle);
}

脚本 - 在 SharePoint 上创建列表项:

var siteUrl = '/sites/MyList';

function createListItem(pollValue, pId, pollTitle) {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('PollCustomResponses');

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

oListItem.set_item('Title', pollTitle);
oListItem.set_item('Response', pollValue);
oListItem.set_item('PollID', pId);

oListItem.update();

clientContext.load(oListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}       

当我单击submit 按钮时,它会按预期创建一个新的列表项。但是,我在 DevTools 中收到以下错误消息:

未捕获的类型错误:无法读取未定义的属性“应用”

错误是检查时突出显示clientContext.executeQueryAsync 行。

【问题讨论】:

    标签: javascript sharepoint-online


    【解决方案1】:

    我通常使用 SP.ClientContext.get_current 来获取当前站点的上下文,我的示例测试脚本。

     <button type="button" class="btn btn-info" id="getPollval">Submit</button>
        <script type="text/javascript">
            document.getElementById("getPollval").onclick = function () {            
                createListItem("Test");
            }
            //var siteUrl = '/';
            function createListItem(pollValue, pId, pollTitle) {
    
                var clientContext = new SP.ClientContext.get_current();//SP.ClientContext(siteUrl);
                var oList = clientContext.get_web().get_lists().getByTitle('MyList');
    
                var itemCreateInfo = new SP.ListItemCreationInformation();
                this.oListItem = oList.addItem(itemCreateInfo);
    
                oListItem.set_item('Title', pollValue);
                //oListItem.set_item('Response', pollValue);
                //oListItem.set_item('PollID', pId);
    
                oListItem.update();
    
                clientContext.load(oListItem);
    
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            }
    
            function onQuerySucceeded() {
    
                alert('Item created: ' + oListItem.get_id());
            }
    
            function onQueryFailed(sender, args) {
    
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        </script>
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2015-01-06
      • 2018-11-09
      • 2017-07-26
      • 2019-02-26
      • 2015-07-11
      • 1970-01-01
      相关资源
      最近更新 更多