0

Currently we are trying to access Schema Id's from within an entity in DXA and are unable to do so. How would we go about accomplishing this? Thanks

update:

We are actually trying to map Component fields that allow multiple Schemas. We use the SemanticProperty to define multiple fields that map to just one field in the Schema.

We are finding that this is not working as expected. We were hoping to access the fields Schema ID or name to make sure we render out the Component link with the appropriate view.

Note: each of the allowed Schemas has their own view.

update 2:

public class Parent : EntityModel
{
    public string id { get; set; }
    [SemanticProperty("content")]
    public List<SchemaA> contentA { get; set; }
    [SemanticProperty("content")]
    public List<SchemaB> contentB { get; set; }
}

SchemaA and SchemaB both have their own view implementation. Class Parent has a component link field called "content" that allows both SchemaA and SchemaB type components.

When accessing the collection contentB, we expected it to be populated only with SchemaB objects. But, both collections, contentA and contentB have the objects loaded (same object in both collections).

In order to render out the right collection with the right view, we would like to access the underlying schema to populate the right collection based on content.

7
  • any specific requirements to access it, Warning: ID is changed when ported to different environment.
    – Raj Kumar
    Aug 8, 2017 at 5:59
  • 1
    It is pretty much possible in DXA. DXA website implementation best practice suggests the website implementation should not rely on tcm ids and as you are trying to fetch the schema id of a DXA entity, I am wondering about the business use case of yours, so we can suggest you a better option than relying on the tcm schema id.
    – Rajesh
    Aug 8, 2017 at 6:03
  • We are actually trying to map component fields that allow multiple schema's. We use the SemanticProperty to define multiple fields that map to just one field in the schema. We are finding that this is not working as expected. We were hoping to access the field's schema ID or name to make sure we render out the component link with the appropriate view. Note: each of the allowed schema's has their own view.
    – imcz
    Aug 8, 2017 at 14:40
  • @imcz could you edit your question and add this information there, it is too important to leave it in a small and unreadable comment. You should form your question around the problem you are encountering, don't try to ask for help on a solution you are thinking of, because then you limit the opportunity of a correct answer. Aug 8, 2017 at 14:55
  • Thanks @BartKoopman. I have added the details within the question itself.
    – imcz
    Aug 8, 2017 at 16:18

1 Answer 1

1

You can achieve this in 2 different ways -

1) Create a common model for the allowed schemas and assign that common model. You can find the sample in reference implementation.

2) I will recomend this one becase this is more strongly typed. Difine your base clase which is type of abstract class and a subclass of EntityModel. Now inherite required model from this base class instead of EntityModel. The DXA model mapping will automatically instantiate the appropriate concrete subclass based on semantic mapping.

Just as an example:

public abstract class MyBaseModel : EntityModel
{
}

[SemanticEntity(EntityName ="SchemaA",Prefix = "s", Vocab = CoreVocabulary)]
public class SchemaA : MyBaseModel
{
...
}

[SemanticEntity(EntityName = "SchemaB", Prefix = "s", Vocab = CoreVocabulary)]
public class SchemaB : MyBaseModel 
{
   ...
}

public class Parent : EntityModel
{
    public string id { get; set; }

    [SemanticProperty("content")]
    public MyBaseModel contentA { get; set; }

}

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.