Question

I'm trying to extract data from a Sharepoint list to create some graphs and I'm in need of a function to extract items from a list where I can insert in parameters such as name of the list, and column names I want to extract. So I could instantiate a function: extractData (listName, ColumnTitle1, ColumnTitle2)to easily extract from different lists by just instantiating that function.

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
Was it helpful?

Solution

Your code is not really clear. What are you trying to push into your array ? Right if you have three columns (ID, Title, Amount) and 3 rows in your list, then your array will look like this

[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

I don't think it makes really sense, doesn't it? Maybe you want to have 1 row = 1 row. So the array will look like this:

[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

If it's the case, your loop will be like that :

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, the Microsoft API is so bad to use ^^ I created an API : http://aymkdn.github.io/SharepointPlus/ easy to use. Your code will look like that with my library :

$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"); 
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top