我正在尝试从Sharepoint列表中提取数据以创建一些图形,我需要一个函数来从列表中提取项目,我可以在列表中插入参数,例如列表名称和列名称I想要提取。所以我可以实例化一个函数:extractData(listName,ColumnTitle1,ColumnTitle2),只需实例化该函数即可从不同的列表中轻松提取。
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var list1 = clientContext.get_web().get_lists().getByTitle("List_Name");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>200</RowLimit></View>');
this.collListItem = list1.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
for (var i = 1; i<column1Titles.length;i++){
myArray1[i].push(oListItem.get_item(column1Titles[i]));
}
}
Graph("holder", global1[1], "High Risk Project", global1[2], "Yes");
// This instantiates my graph, I need something like this but to extract the data.
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
} // end of fail
答案 0 :(得分:0)
您的代码并不是很清楚。你想把什么推进你的阵列?如果您的列表中有三列(ID,标题,金额)和3行,那么您的数组将如下所示
[0] = 1; // ID
[1] = "Some stuff"; // Title
[2] = 25; // amount
[3] = 2; // ID
[4] = "Something"; // Title
[5] = 15; // Amount
[6] = 3; // ID
[7] = "Something else"; // Title
[8] = 36; // Amount
我认为它没有意义,不是吗?也许你想要1行= 1行。所以数组看起来像这样:
[0][0] = 1;
[0][1] = "Some stuff"; // Title
[0][2] = 25; // amount
[1][0] = 2; // ID
[1][1] = "Something"; // Title
[1][2] = 15; // Amount
[2][0] = 3; // ID
[2][1] = "Something else"; // Title
[2][2] = 36; // Amount
如果是这样的话,你的循环将是这样的:
var myArray1=[],j=0;
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
myArray1[j]=[];
for (var i = 1; i<column1Titles.length;i++){
myArray1[j].push(oListItem.get_item(column1Titles[i]));
}
}
BTW,Microsoft API使用起来很糟糕^^我创建了一个API:http://aymkdn.github.io/SharepointPlus/易于使用。您的代码与我的库一样:
$SP().list("list_name").get({fields:column1Titles, rowlimit:"200", where:"ID >= 1"}, function(data) {
var myArray1=[];
for (var i=0, len=data.length; i < len; i++) {
myArray1[i]=[];
for (var c=0; c < column1Titles.length; c++)
myArray1[i].push(data[i].getAttribute(column1Titles[c]));
}
Graph("holder", global1[1], "High Risk Project", global1[2], "Yes");
})