当用户单击按钮时,我正在尝试创建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中收到以下错误消息:
未捕获的TypeError:无法读取未定义的属性“应用”
检查时错误突出显示clientContext.executeQueryAsync
行。
答案 0 :(得分:0)
我通常使用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>