1

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...

1

There is no simple generic way. Even SharePoint uses diffrent renderer for each fieldtype (here's a list CSR View Fields dynamic with Field names from View?)

        Computed: new ComputedFieldRenderer(field.Name),
        Attachments: new AttachmentFieldRenderer(field.Name),
        User: new UserFieldRenderer(field.Name),
        UserMulti: new UserFieldRenderer(field.Name),
        URL: new UrlFieldRenderer(field.Name),
        Note: new NoteFieldRenderer(field.Name),
        Recurrence: new RecurrenceFieldRenderer(field.Name),
        CrossProjectLink: new ProjectLinkFieldRenderer(field.Name),
        AllDayEvent: new AllDayEventFieldRenderer(field.Name),
        Number: new NumberFieldRenderer(field.Name),
        BusinessData: new BusinessDataFieldRenderer(field.Name),
        Currency: new NumberFieldRenderer(field.Name),
        DateTime: new DateTimeFieldRenderer(field.Name),
        Text: new TextFieldRenderer(field.Name),
        Lookup: new LookupFieldRenderer(field.Name),
        LookupMulti: new LookupFieldRenderer(field.Name),
        WorkflowStatus: new RawFieldRenderer(field.Name)

You could also build several renderer since there arent that many types.

But if you want to render an HTML table with the data for that list - you know that a ListViewWebPart does exactly that?

  • I want more control of how the data is rendered so ListViewWebPart does not do what I need. – IMTheNachoMan Oct 19 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.