3

Is it possible to retrieve a collection of fields as a property of a mapped glass model ? For example, I have a "Product" item in Sitecore with a collection of custom "Product Attribute" fields. I'd like to be able to access these fields as a collection through which I can enumerate, without explicitly naming the fields in my glass model. This way I can then add more attributes and enumerate them in code, without changing the glass model code.

Something like the following:

[SitecoreInfo(SitecoreInfoType.Fields)]
public virtual IEnumerable<something> Attributes { get; set; }

Many thanks.

epik phail
  • 195
  • 8
  • When you say "Product Attribute" fields, I presume they are actually other Sitecore items located elsewhere in the tree (selectable via a multilist or similar)? – jammykam Aug 18 '15 at 06:12
  • They are custom field types which are fields of the Product template. They are not child items. – epik phail Aug 18 '15 at 08:02
  • If they are fields on your template (default or custom) then other than using reflection I am not aware of any way (and I would avoid the reflection route). Perhaps use some sort of code generation tools instead, or rethink your design. – jammykam Aug 18 '15 at 15:40
  • @epikphail Maybe change the Information Architecture and force them to select this items in a Treelist or multilist if it possible. By doing this it will be possible to get them as a list and your code will work :) – nsgocev Aug 19 '15 at 04:55
  • @nsgocev that would be sensible, but there are some other requirements which make this a preferable approach. Thanks for reply. – epik phail Aug 19 '15 at 06:02
  • @epikphail then custom handler implementation should be your way to go ! :) – nsgocev Aug 19 '15 at 10:57

2 Answers2

2

Without any more specifics, it sounds like you may want to implement a Glass Mapper data handler via AbstractSitecoreFieldMapper for your custom type.

Jim Noellsch
  • 714
  • 4
  • 7
  • Thanks I will investigate that. – epik phail Aug 19 '15 at 01:26
  • 1
    This was a good solution. I wrote a custom data handler which returned a collection of Product Attribute fields. It was necessary to add a text field to the Product template in order to convince Glass to do the mapping. – epik phail Aug 20 '15 at 01:33
-2

You would actually need something like

[SitecoreInfo(SitecoreInfoType.Fields(Filters = "*.customAttributes", Order ="..."))]

because you have to define the order of the collection and a way how to recognise the items to be included into the collection. Better don't do it and look out for a cleaner solution.

For example you could specify the product attributes as child items or just give the product attributes real names and reference them in the sublayout/view.

Florian Sc
  • 102
  • 6
  • 1
    Where did `SitecoreInfoType.Fields` come from? It's not in the list of valid enum values for [SitecoreInfoType](https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/Configuration/SitecoreInfoType.cs) – jammykam Aug 18 '15 at 15:37
  • It's just a creatively invented pseudo code.Thought I've mentioned it. – Florian Sc Aug 19 '15 at 11:28
  • 1
    Unfortunately creatively invented pseudo codes don't solve problems :( – jammykam Aug 19 '15 at 12:15