@jayspectech You have a devious mind! That’s like naming your dog, “Cat”. I love it!
— Bob German (@Bob1German) September 16, 2016
Sometimes I feel bad for junior front-end developers who think they have a decent handle on things, and then get thrown into the world of SharePoint. They don’t have the SharePoint Spidey-sense that tells them something simple is going to go horribly wrong.
I was called on to assist a junior developer who was tearing his hair out trying to do simple CRUD operations on list items in SharePoint in an Angular application. I’d already written a (somewhat) simplifying Angular service to deal with most of the annoying parts of the REST API, so I was baffled that he was having so much trouble with it.
So I stepped into the code with him, making sure he was calling my service correctly. He was. I began to panic. Was there a bug in my service? I stepped into the service code. Everything looked fine, and SharePoint returned the proper “201 CREATED” response. But every field except one was being set.
The name of that field was “DisplayName”.
I proceeded to create a new Custom List called “TestThisBug.” I created a field called “DisplayName” and another field called “FooDisplayName.”
Simple, right? Then I tried to get the fields via the REST API.
The field did not appear. Curiouser and curiouser.
I tweeted the tweet above, and told the developer to name the field something else and be on his way. I wondered, “What if we try the same operations using the JSOM?”
So, on my SharePoint 2013 VM, I fired up a Site Collection, created my list, and wrote me some JSOM code. I wrote some code that will get that list item, select a few fields (including “DisplayName”) and just display it in a table.
I got every field I asked for, except for “DisplayName” which was silently disregarded. When I tried to ask for it directly via
get_item('DisplayName') I was told it wasn’t initialized, despite the fact that I’d explicitly requested it.
I began to wonder if I would run into the same creation issue my coworker was encountering. I wrote some more JSOM code to actually create a list item with the “DisplayName” field set. And, even more strangely… It worked!
I then logged on to my Office 365 Dev Tenant to see if this is still an issue.
Yup! Sure is! You can see here that I can get the field name from the list via
list.get_fields(), but when I return list items, the field is not returned, despite explicitly requesting it. You can also see that the “DisplayName” field that is returned from
get_fields() doesn’t have any super-secret internal name that I’m missing.
But, as before, adding a list item with that field via JSOM works just fine:
So, kids, I guess the moral of this story is pretty simple: Don’t name your fields “DisplayName.”
Here’s the bug-test JSOM code.