2

I'm using Sitecore 9.0 Update 2 with Glass Mapper.Sc version 5.2.13

I'm trying to create items in Sitecore using GlassMapper and the following code

repository.RequestContext.SitecoreService.CreateItem(parent, model);

But I get this error:

Type 'MyModel' does not have a Template ID or Branch ID.

MyModel is decorated with the [SitecoreType(TemplateId = "{my-model-template-guid}", AutoMap = true)] attribute and inherits from a base class that has a TemplateId property defined as

[SitecoreInfo(SitecoreInfoType.TemplateId)]
public virtual Guid TemplateId { get; set; }

but it does not have the '[SitecoreType(TemplateId = "{base-class-template-guid}", AutoMap = true)]' defined ... could be this the issue ? If I try creating items based on the base class model it works just fine.

All MyModel's properties are virtual but it seems to me the GlassMapper does not read the TemplateId specified as attribute.

Has anyone experienced this?

Update: Adding Model Classes:

Please see the model code below

[SitecoreType(TemplateId = "{AA1DE664-7E99-4ACB-8DB4-EBA8C3F0693E}", AutoMap = true)]
public class MyModel : GlassMapperBase
{
    [SitecoreField(FieldName = "First Name")]
    public virtual string FirstName { get; set; }

    [SitecoreField(FieldName = "Last Name")]
    public virtual string LastName { get; set; }
}

And the base class code together with the interface it implements:

public class GlassMapperBase : IGlassMapperBase
{
        [SitecoreId]
        public virtual Guid Id { get; set; }

        [SitecoreInfo(SitecoreInfoType.Version)]
        public virtual int Version { get; set; }

        [SitecoreInfo(SitecoreInfoType.Language)]
        public virtual Language Language { get; set; }

        [SitecoreInfo(SitecoreInfoType.Name)]
        public virtual string Name { get; set; }

        [SitecoreInfo(SitecoreInfoType.ContentPath)]
        public virtual string ContentPath { get; set; }

        [SitecoreInfo(SitecoreInfoType.FullPath)]
        public virtual string FullPath { get; set; }

        [SitecoreInfo(SitecoreInfoType.Url)]
        public virtual string Url { get; set; }

        [SitecoreInfo(SitecoreInfoType.DisplayName)]
        public virtual string DisplayName { get; set; }

        [SitecoreInfo(SitecoreInfoType.TemplateName)]
        public virtual string TemplateName { get; set; }

        [SitecoreInfo(SitecoreInfoType.TemplateId)]
        public virtual Guid TemplateId { get; set; }

}

public interface IGlassMapperBase
{
    [SitecoreId]
    Guid Id { get; set; }

    [SitecoreInfo(SitecoreInfoType.Version)]
    int Version { get; set; }

    [SitecoreInfo(SitecoreInfoType.Language)]
    Language Language { get; set; }

    [SitecoreInfo(SitecoreInfoType.Name)]
    string Name { get; set; }
}
2
  • Could you please add code snippets of your model classes to your question? Jan 16, 2019 at 13:48
  • 1
    Not sure why you are getting that error, but here is a suggestion. Just use the Sitecore API to create the item. Don't bother with Glass for that. It will be quicker to write and you won't get cryptic errors like this.
    – Richard Seal
    Jan 16, 2019 at 16:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.