REST API and non-required Document Field

Yang Wen asked on February 1, 2016 19:31

I'm using the REST API to retrieve specific fields from our content pages.

For our custom Document Type, we've specified custom fields that are non-required, as a result, not all the documents have this field populated. I'm sure this is a typical scenario for many sites.

It appears for non populated custom fields, the REST API will omit the field from the response, for the object without that field populated, as opposed to all the default Document fields that are always present, regardless of their value.

As a result, a simply query containing ?columns=OptionalField is failing with Error: Invalid column name 'OptionalField'

How are you dealing with this? Am I missing a certain flag for these non populated fields to return?

EDIT: Solution ended up being that I had to specify classnames parameter in the REST URL to constraint my query to a single page Type.

Correct Answer

Roman Hutnyk answered on February 1, 2016 21:06

Okay, my friend, you're not right. See the request/response for multiple documents:

http://localhost/Kentico82/rest/content/currentsite/en-us/childrenof/Articles?classnames=DancingGoat.Article&columns=Articletitle,Articlesummary,dummy,test

{"cms_documents": 
[
{"DancingGoat_Article": 
[
{"test":null,"Articlesummary":"Loss of interest in activities. Decreased energy. Those are just some of the well-known, telltale signs of depression. But depression doesn’t always mean unbearable sadness.","dummy":null,"Articletitle":"7 Surprising Signs You May Be Depressed"}
,{"test":null,"Articlesummary":"\r\nCancer and Identity: A Patient’s Journey to Finding Herself\r\nCancer and Identity: A Patient’s Journey to Finding Herself\r\nA question I’ve been asking since my cancer diagnosis: “Who am I?”","dummy":null,"Articletitle":"Cancer and Identity: A Patient’s Journey to Finding Herself"}
,{"test":null,"Articlesummary":"With fall just around the corner comes the seasonal food switch—from light summer fare to hearty, comfort foods. And what’s more comforting than a heaping helping of mashed potatoes?","dummy":null,"Articletitle":"Broccoli-potato Mash-up: Comfort Food Reimagined"}
,{"test":null,"Articlesummary":"FLINT, Mich. – March 17, 2015 – As part of a limited panel, the nation’s largest independent specialty pharmacy will offer PARI’s cystic fibrosis treatment, a 28-day supply of Tobramycin Inhalation Solution together with a PARI LC PLUS® nebulizer handset.","dummy":null,"Articletitle":"Diplomat Selected to Dispense KITABIS™ PAK for Cystic Fibrosis"}
,{"test":null,"Articlesummary":"No one likes having to do weekly (or daily) injections. Some hurt, some sting, some smell, some make us sick, and all make us uncomfortable to some degree, but...","dummy":null,"Articletitle":"Self-injection"}
]
}
]
}

http://localhost/Kentico82/rest/content/currentsite/en-us/document/Articles/Origins-of-Arabica-Bourbon?columns=Articletitle,Articlesummary,dummy,test

{"cms_documents": 
[
{"DancingGoat_Article": 
[
{"test":null,"Articlesummary":"FLINT, Mich. – March 17, 2015 – As part of a limited panel, the nation’s largest independent specialty pharmacy will offer PARI’s cystic fibrosis treatment, a 28-day supply of Tobramycin Inhalation Solution together with a PARI LC PLUS® nebulizer handset.","dummy":null,"Articletitle":"Diplomat Selected to Dispense KITABIS™ PAK for Cystic Fibrosis"}
]
}
]
}

So as you can see I'm requesting ?columns=Articletitle,Articlesummary,dummy,test but neither list nor single responses return Dummy and Test fields, however I'm getting response.

So do not look for workaround, but find your issue. Unfortunately I do not have enough input to help you with this.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on February 1, 2016 20:04

Double check column names. It does not work that way and returns null for not populated columns. Following is example response in one of my applications:

{"cms_documents": 
[
{"DC_ClaimUnit": 
[
{"DyelotOrdered":null,"Manager":-1,"ParentRollBalance":null,"DyelotReceived":null,"InventoryAdjustments":null,....}
,{"DyelotOrdered":null,"Manager":-1,"ParentRollBalance":null,"DyelotReceived":null,"InventoryAdjustments":null,....}
]}]}
0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on February 1, 2016 20:13 (last edited on February 1, 2016 20:17)

Double check column names. It does not work that way and returns null for not populated columns.

Yes I've observed that behavior on custom tables. The REST API returns NULL when column is not populated for a given row.

However for Documents, the REST API's behavior is different. If I have custom field for some Document Type, and if that custom field is not populated for a row, it will simply omit the field in the returned object. This is also why I'm getting the invalid column name error. For Document fields (non custom field), the response object will always contain the column, regardless whether it's populated.

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on February 1, 2016 21:15 (last edited on February 1, 2016 21:18)

Thanks for testing it on your end.

I see that you're using the classnames parameter to qualify the page type. I wasn't doing that.

I changed my URL to specify the class name and now the invalid column error is no longer happening. This additional parameter makes sense since the child path I'm targeting is not 100% homogeneous in terms of Page Type.

I also realized that the null value return is a behavior applicable to the JSON format. I had been using the default XML format, which DOES omit custom elements that have no value, The omission made it confusing in combination with the invalid column error message.

Now the mystery is solved! Thanks!

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.