我是Javascript的新手,但我尝试创建一个代码,访问Sharepoint-list。我有这段代码来访问列表"Mytasks",我想要获取select-tag中的项目。现在它只显示列表的第一项。我找不到问题所在。
// load all necessary sharepoint javascript libaries
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
// load the sharepoint list.
loadSharepointList();
});
// loads the sharepoint list
function loadSharepointList() {
// create the sharepoint content.
var context = SP.ClientContext.get_current();
// get the list by the title.
var list = context.get_web().get_lists().getByTitle('myTasks');
// create the query.
var caml = new SP.CamlQuery();
caml.set_viewXml("<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>");
// get the list items asynchronously
var listItems = list.getItems(caml);
context.load(listItems, 'Include(Title)');
context.executeQueryAsync(
// success delegate
Function.createDelegate(this, function () {
// loop through the items.
var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
// get the current list item.
var listItem = listEnumerator.get_current();
// get the field value.
var fieldValue = listItem.get_item('Title');
var list = document.getElementById("tstList");
var option = document.createElement("option");
option.text = fieldValue;
list.add(option);
// console.log(fieldValue);
return "<option>" + fieldValue + "</option>";
}
}),
// error delegate
Function.createDelegate(this, function () {
alert('Error fetching data from Sharepoint!');
}));
}
有什么想法吗?
发布于 2017-06-12 17:30:30
我解决了我的问题。我将行var list = document.getElementById("tstList");移到了while循环之前,并将返回移到了循环之后。所以现在一切都好了。谢谢你的回答。
https://stackoverflow.com/questions/44485258
复制相似问题