1

We upgraded Glass Mapper version from 4.2.1.190 to 5.4.21.0. In our model, we are using [SitecoreChildren(InferType = true)] to get the child items. For few of the child items, language fallback is disabled and they do not have any version in target languag say en-us. So we want to display only the items which has atleast one version. This was working fine in Glass Mapper 4 but after upgrading to Glass Mapper 5 we observed that SitecoreChildren is listing out the items even if the version count is 0 and language fallback is disabled.

Also, Enforce Language Fallback is disabled.

Models:

public class Product 
{
    [SitecoreField(FieldName = "Image List")]
    public virtual ImageList ImageListItem { get; set; }
}

public class ImageList 
{
    [SitecoreChildren(InferType = true)]
    public virtual IEnumerable<ImagesComponent> Images { get; set; }
}

View:

@model ImageList
foreach(var image in model.Images)
{
  --logic --
}

Am I missing any setting that needs to be changes in Glass Mapper 5?

1
  • I tried enabling enforce version presence on template as mentioned in above question but no luck. Also, even if version presence is disabled, it is working as expected in glass 4 but not in glass 5
    – Shiva
    Jul 5, 2019 at 13:48

1 Answer 1

2

In Glass Mapper v5, the determination for whether an item has versions has changed from using Sitecore.Data.Items.Item.Versions.Count to checking if Sitecore.Data.Items.Item[Sitecore.FieldIDs.Revision] is null or empty. It's possible that this new method of checking the existence of the __Revision field is potentially inaccurate in your solution and instance of Sitecore, causing Glass to map those items without versions but still maintaining a value in the __Revision field.

To revert to the legacy Sitecore.Data.Items.Item.Versions.Count determination, you'll need to update the Glass dependency resolver to use ItemVersionCountTask instead of the default ItemVersionCountByRevisionTask. To do so, you'll need to add the following code into your GlassMapperScCustom.cs file in its CreateResolver() method:

public static IDependencyResolver CreateResolver(){
    var config = new Glass.Mapper.Sc.Config();

    var dependencyResolver = new DependencyResolver(config);

    // ADD THE LINE BELOW TO REPLACE THE ITEM VERSION COUNT TASK
    dependencyResolver.ObjectConstructionFactory.Replace<ItemVersionCountByRevisionTask, ItemVersionCountTask>(() => new ItemVersionCountTask());

    return dependencyResolver;
}

Glass Mapper Documentation: http://www.glass.lu/mapper/documentation/Upgrade-ToV5

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.