11

I'm working on a project in which we are upgrading an instance of Sitecore from v6.6 to v8.1. The original code built on top of v6.6 is directly accessing the Sitecore.Data API (as opposed to Glass Mapper or some other common ORM). I found one strange behavior post-upgrade tied to this snippet of code.

var value = item["Name"];

The field in question was defined by a custom template/field simply named "Name". This is not related to the standard field Name which is actually, to the best of my knowledge, stored as "__Name". From what I could find while running a debugger, the API returns null even though the item in question has a value for that field. In the same debugger session, I explored the contents of the item.Fields collection and there was no entry for "Name". There were, however, entries for other fields defined by that template. It's as if Sitecore skipped that specific field.

Meanwhile, if I tried to retrieve that field's value via the field definition's GUID, it actually returned the value I was looking for:

var value = item["{64CC3858-18DA-43DA-A7AE-86C1DDEC316D}"];

I can live with this workaround. But for future reference and possibly the benefits of others, are there known field names that should never be used because Sitecore won't retrieve them through normal means? In other words, are there "reserved" field names?

Edit: As it turns out, this ended up being a publishing issue. I don't know how but the field section to which the field in question was a child didn't have any versions and, thus, Sitecore skipped it during publishing. Once I added an English version to that item and republished the entire template, the field and its appropriate value were available from both item["Name"] and item.Fields["Name"]. It is strange that using the GUID was a valid workaround in this situation since the field definition item didn't appear to exist in the web database.

Thanks everyone for your help!

2
  • Do you use language fallback? I believe the behavior of getting a field by name and by ID will differ depending on whether the field exists in the current language. Nov 17, 2016 at 8:00
  • @Chris can you please show a screen shot of the Builder tab on the template showing the "Name" field, or the Field item itself so we have some more information to diagnose the problem?
    – Richard Seal
    Nov 17, 2016 at 16:15

3 Answers 3

10

There definitely are reserved field names. Any field on the Standard Template should not be used. I would also recommend you use an Id where possible.

You can see the use of this pattern in the Sitecore.Kernel.dll where fields found in the FieldIDs class are used when accessing standard Sitecore fields. The use of the Id will of course be fast as well as future proof.

Example from DotPeek

This SO question also contains several answers recommending the use of IDs.

John also has a good explanation here about how the Standard Template is composed.

2
  • While I agree from a recommended practices standpoint, there really should not be any reserved names from a technical perspective. And certainly not "Name" since item.Name is, in fact, not a field at all. I've spent the better part of 2 hours now trawling the Item API looking for a "why" to the question "why does item['name'] not return a result?" and have to call it a night :P Nov 16, 2016 at 23:01
  • My guess would be when accessing by fieldName the TemplateManager is called and unable to locate the field. Nov 16, 2016 at 23:24
4

@Chris Elston, This looks like a issue with field name? I tried to mimic your scenario. My setup is on Sitecore 8.2.

  • Updated the sample item 'Title' field to 'Name'
  • Did a Smart Publish on Home item along with related items option selected
  • Debugged the code

Name field accessed from Item

I am able to see the field value. If you are not able to see the field itself under all fields, I doubt

  1. it could be that template field is not published
  2. or the item version being accessed doesn't have the field
  3. the field display name is set as "Name" but the item name is something else. In this case the assessor with "ID" will work.

check @Mark Cassidy response here

To support the 3rd point above, see below images

Field item with different displayname from item name field item with different displayname from item name

Access field by item name - No Value Access field by item name - No Value

Access field by item id - Has value Access field by item id - Has value

2
  • 1
    Turned out being your first point. See the edit to my question. Thanks! Nov 17, 2016 at 17:28
  • @ChrisElston good to know it helped. I was sure the 3rd option might be the answer in your case. but surprising it is the first one!!
    – phani
    Nov 19, 2016 at 14:35
0

If you want to retrieve all the fields when running the item.Fields, you need to call following command before that.

Item.Fields.ReadAll();

This will instruct sitecore to take all the fields in the item.

I think following link explains the same senario you are facing http://sitecoresolution.blogspot.com/2016/07/How-to-Read-all-fields-from-sitecore.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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