Glass.Mapper.Sc | You can not set an empty or null Item name

Today I was writing an import script for inserting content into Sitecore.  I was using the Glass.Mapper.Sc API instead of the Sitecore API (there are a lot of benefits to using Glass.Mapper.Sc for everything you can instead of the Sitecore API because it allows you to code against interfaces which allows you to do IoC and implement unit tests for your business logic in your Sitecore implementation).  I will be writing a blog post about Glass.Mapper.Sc vs Sitecore API soon.

This post today is regarding an error I encountered when trying to import content.  The error was:

You can not set an empty or null Item name

I searched on Google and found that Dan Solovay reported the same issue and then replied explaining how he solved it.

When you’re creating or updating Glass objects using Glass.Mapper.Sc.ISitecoreService.Save, you need to make sure the Name property in your Glass objects has a public set method.  Here’s an example:

public partial interface IGlassBase {
    [SitecoreInfo(SitecoreInfoType.Name)]
    string Name { get; set; }
}

public abstract partial class GlassBase : IGlassBase {
    [SitecoreInfo(SitecoreInfoType.Name)]
    public virtual string Name { get; set; }
}

If you’re using the TDS Code Generation feature and you’re going to use the Glass.Mapper.Sc.ISitecoreService to import content into Sitecore, you’ll need to make sure that your glassv3header.tt template includes the public set method as shown in the example above.

Here’s an link to a glassvheader.tt that contains the updated Name property – https://github.com/dcouto/ScWebApi/blob/master/ScWebApi.TDS.Master/Code%20Generation%20Templates/glassv3header.tt


                
                            

Leave a Reply