javascript - CSOM 无法通过 SP 未定义
问题描述
我检查了这个网站和其他网站,找到了我尝试过的答案,但一定是缺少正确的答案。首先冒险进入 CSOM,因为我通常在 C# 服务器端做这种事情。正如您在 getScript 函数中看到的那样,我已将我能想到的所有内容都拉了下来,但仍然没有好处。文档声称已准备就绪,我通过写信给 console.log 来确认。请问我错过了什么。错误出现在函数retrieveListItems() 的第二行。
<html>
<head>
<title></title>
<script src="scripts/jquery-1.7.2.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="scripts/MicrosoftAjax.js">
</script>
</head>
<body>
<script type="text/javascript">
var hostweburl;
// Load the required SharePoint libraries.
// Get the URI decoded URLs.
hostweburl = "https://hubble.whanganui.govt.nz";
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to
// the execOperation function.
$.getScript(scriptbase + "init.js");
$.getScript(scriptbase + "MicrosoftAjax.js");
$.getScript(scriptbase + "sp.core.js");
$.getScript(scriptbase + "sp.runtime.js");
$.getScript(scriptbase + "sp.js");
$(document).ready(function(){
//ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
console.log("ready to roll");
retrieveListItems("https://path_to_my_SP_site")
});
// Continue your program flow here.
function retrieveListItems(siteUrl){
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
'<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nBody: ' + oListItem.get_item('Body');
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
</script>
</body>
</html>
解决方案
你可以试试这个。
<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<script type="text/javascript">
var hostweburl;
// Load the required SharePoint libraries.
// Get the URI decoded URLs.
hostweburl = "http://sp:12001";
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to
// the execOperation function.
//$.getScript(scriptbase + "init.js");
//$.getScript(scriptbase + "MicrosoftAjax.js");
//$.getScript(scriptbase + "sp.core.js");
//$.getScript(scriptbase + "sp.runtime.js");
//$.getScript(scriptbase + "sp.js");
$(document).ready(function () {
//ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
$.getScript(scriptbase + 'init.js', function () {
$.getScript(scriptbase + 'MicrosoftAjax.js', function () {
$.getScript(scriptbase + 'sp.core.js', function () {
$.getScript(scriptbase + 'SP.Runtime.js',
function () {
$.getScript(scriptbase + 'SP.js', function () {
retrieveListItems("http://sp:12001")
})
});
});
});
});
});
// Continue your program flow here.
function retrieveListItems(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList2');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
'<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title');
//'\nBody: ' + oListItem.get_item('Body');
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
</script>
</body>
</html>
推荐阅读
- python - 为什么仍然给出类型错误?
- cytoscape.js - cytoscpae.js 的 cos-bilkent 布局扩展
- symfony - Symfony - 成功登录后保护身份验证器丢失用户
- c - 无法使用 C30 编译器复制 PIC24FJ256GB206 中的数组
- r - dput 的 row.names 与 rownames() 返回的不一致。为什么?
- apache-kafka - 卡夫卡 | 如何使用replica.high.watermark.checkpoint.interval.ms
- linux - 如何在 Shell 脚本中多次检查条件?
- php - 在 Laravel 应用程序中放置全局 php 的位置
- html - 完美放置比屏幕更大的图像
- android - 安装应用程序时调用接收器