I'm using the below code to get the fields for a view, then all the rows for a list and saving the value of those fields. My issue is that some fields will return an object and I'm trying to figure out how I get the value from those objects.
The code:
var out = [];
ExecuteOrDelayUntilScriptLoaded(function()
{
var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("Meeting Agenda Items");
var view = list.get_views().getByTitle("test");
this.viewFields = view.get_viewFields();
clientContext.load(viewFields);
clientContext.executeQueryAsync(Function.createDelegate(this, viewFieldsLoaded), Function.createDelegate(this, function(){alert("error 1");}));
}, "sp.js");
function viewFieldsLoaded()
{
this.fields = [];
var fieldEnumerator = this.viewFields.getEnumerator();
while(fieldEnumerator.moveNext())
{
this.fields.push(fieldEnumerator.get_current());
}
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Meeting Agenda Items');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>5</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(' + fields.join(',') + ',EffectiveBasePermissions)');
clientContext.executeQueryAsync(Function.createDelegate(this, rowsLoaded), Function.createDelegate(this, function(){alert("error 2");}));
}
function rowsLoaded()
{
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext())
{
var thisRow = {};
var oListItem = listItemEnumerator.get_current();
for(var i = 0; i < fields.length; ++i)
{
thisRow[fields[i]] = oListItem.get_item(fields[i]);
}
out.push(thisRow);
}
}
At this point, as an example, out
is;
out = [
{
"Title" : "some string",
"ID" : 1,
"People Lookup" : [{...}, {...}, {...}, {...}, ...]
}
];
That People Lookup
field is an array of objects. This is just one example. Different fields will have different types so the result might be different.
I'm a little lost here.
I'm trying to make a generic function that'll take a list name and a view name and render an HTML table with the data for that list...