以下代码完美无缺
$(document).ready(function () {
var scriptbase = "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js");
ExecuteOrDelayUntilScriptLoaded(ready, "SP.Runtime.js");
})
function ready() {
var scriptbase = "/_layouts/15/";
$.getScript(scriptbase + "SP.js", function(){
var d = $.Deferred();
var path = document.location.href
console.log(path);
var context = new SP.ClientContext('http://myserver/centrodeprocesos/procesos');
var oList = context.get_web().get_lists().getByTitle('ImagenesDeFondo');
var camlQuery = SP.CamlQuery.createAllItemsQuery();
var collTermListItem = oList.getItems(camlQuery);
context.load(collTermListItem);
var o = {d: d, collTermListItem:collTermListItem};
context.executeQueryAsync(
Function.createDelegate(o, onQuerySucceeded),
Function.createDelegate(o, onQueryFailed));
return d.promise();
});
}
function onQuerySucceeded()
{
var listItemEnumerator = this.collTermListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
console.log(oListItem.get_item('Title'));
//console.log(oListItem.get_item('Url'));
}
this.d.resolve(oListItem);
}
function onQueryFailed() {
this.d.reject("something bad happened");
}
但是我还需要打印URL,这是列表中的一个字段
$(document).ready(function () {
var scriptbase = "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js");
ExecuteOrDelayUntilScriptLoaded(ready, "SP.Runtime.js");
})
function ready() {
var scriptbase = "/_layouts/15/";
$.getScript(scriptbase + "SP.js", function(){
var d = $.Deferred();
var path = document.location.href
console.log(path);
var context = new SP.ClientContext('http://myserver/centrodeprocesos/procesos');
var oList = context.get_web().get_lists().getByTitle('ImagenesDeFondo');
var camlQuery = SP.CamlQuery.createAllItemsQuery();
var collTermListItem = oList.getItems(camlQuery);
context.load(collTermListItem, 'Include(Url)');
var o = {d: d, collTermListItem:collTermListItem};
context.executeQueryAsync(
Function.createDelegate(o, onQuerySucceeded),
Function.createDelegate(o, onQueryFailed));
return d.promise();
});
}
function onQuerySucceeded()
{
var listItemEnumerator = this.collTermListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
console.log(oListItem.get_item('Title'));
console.log(oListItem.get_item('Url'));
}
this.d.resolve(oListItem);
}
function onQueryFailed() {
this.d.reject("something bad happened");
}
错误是属性尚未初始化或需要明确请求。
答案 0 :(得分:2)
SP.ClientContext.load method允许指定必须请求的属性。
在您提供的示例中,仅请求Url
属性:
context.load(collTermListItem, 'Include(Url)');
但结果除外两个属性:
console.log(oListItem.get_item('Title')); //<--this property is not initialized
console.log(oListItem.get_item('Url'));
因此,解决方案是请求两个属性:
context.load(collTermListItem, 'Include(Title,Url)');
示例强>
以下示例演示了如何从Images
库中检索标题和网址属性:
var listTitle = 'Images';
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items,'Include(Title,FileRef)');
ctx.executeQueryAsync(
function () {
for(var i = 0; i < items.get_count();i++) {
var item = items.getItemAtIndex(i);
console.log(item.get_item('Title'));
console.log(item.get_item('FileRef'));
}
},
function (sender, args) {
//Error handling goes here..
});