SharePoint multiple selection choice field returns "object object"

SharePoint multiple selection choice field returns "object object"

mccdgmccdg Posts: 5Questions: 1Answers: 0
edited March 2016 in Free community support

I am at a loss on how to proceed with this as I am still very new to all of this (jquery/REST/etc.). I have working DataTables pulling from a SharePoint 2013 list using the REST API and am able to do basic selects/filters and expands on lookup fields. However now I am trying to display responses from a choice field (Mod_Reason) where multiple selections are allowed (checkboxes). When I reference the field as a standard field "Object Object" is displayed in the datatable. I have tried various code snippets I have found online but nothing working yet. This is an example of how the data is returned from the REST query:

<d:Mod_Reason m:type="Collection(Edm.String)">
  <d:element>Deobligate</d:element> 
  <d:element>Closeout</d:element> 
 </d:Mod_Reason>

I think I need to use an expand statement in the query but when I do I get a "field not found" message. I have tried things like this that I found online:

{ "data": "Mod_Reason",
 "render": data.d.results.forEach(function(d){
    d.multi = d['Mod_Reason'].results.map(
      function(s){return s.Value}).join(', ');
})}

But that returns nothing.

Any ideas greatly appreciated. I cannot link to the site/data - it is an internal site.

Thanks,
Drew

Replies

  • mccdgmccdg Posts: 5Questions: 1Answers: 0

    For anyone else that might need to do this with SharePoint multi-choice fields using the REST API (not ListData.svc):

    The way the results are returned with the _api call differ from what _vti_bin/listdata.svc returned. You no longer need the $expand statement in the query and it will break if you include it.

    Here is the explanation from the colleague that assisted me:

    So, the result was NOT a collection of lists of strings. It was a collection of collections, of which one of the collections (data.results) was a list of strings.

    Here was the function that wound up working:

    { "data": "Mod_Reason",
    "render": function( data, type, full, meta) {
    if(!data){var returnText = "";}
    else {var returnText = data.results;}
    return returnText;
    }

    So what I’m doing here is first checking to make sure the value isn’t null and, if it is, passing a blank string. If it IS null, and you do not test for it, then it will break and the table will not display. Otherwise, it grabs the results subvariable of Mod_Reason (data.results) and assigns that value to the return variable. At that point, the variable is a string, so if you want to do any kind of formatting, standard string functions like replace should work.

    Hope this helps someone else!

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Thanks for posting back with your findings!

    Another option would be to use:

    {
      data: "Mod_Reason.results",
      defaultContent: ''
    }
    

    columns.defaultContent is used when the data asked for is null or undefined.

    Allan

This discussion has been closed.