Santa

Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Using Glass Mapper Item/Datasource children getting null

September 22, 2022
Using Glass Mapper Item/Datasource children getting null
Keyur Garala
Keyur Garala
Sitecore Certified Architect
using-glass-mapper-item-datasource-children-getting-null
Summary

When we were trying to fetch the child of the datasource item, it was getting a null value for the child. After so much of research, we got to know about the Lazy Loading issue and this has been coming up because of enabling the lazy loading. So, we are trying to turn off the lazy loading by the below method.

[SitecoreChildren(IsLazy = false)]

IEnumerable Children{get;set;}

The above code is working only if you are using below v5 of glass mapper. But in version 5 or more (v5), the glass mapper has removed this method from here. So we can use an alternative method to turn off lazy loading. Please check the below code.

var datasource = _mvcContext.GetDataSourceItem(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.Disabled });

We resolved the issue using this method.

These changes are working for us but after some days, we are facing the error as mentioned below and it instructs us to enable lazy loading but but enabling it, we do not get children of the datasource.

Below is the error please check.

Error:

using-glass-mapper-item-datasource-children-getting-null-1.png

Error in text:

Model depth check failed. Model graph too large, enable lazy loading. Type requested:

Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding
Description: An unhandled exception occurred.

Exception Details: Glass.Mapper.MapperStackException: Model depth check failed. Model graph too large, enable lazy loading. Type requested:

Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IServicew
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding

Solution:

By doing R&D, we got the option which is given in lazy loading “OnlyReferenced”. We applied this option on lazy loading and then the issue was resolved.

var datasource = _mvcContext.GetDataSourceItem<IInterfaceClass>(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.OnlyReferenced }); 
                        

This error occurred because lazy loading disables the item nested child and so on till the 8th child comes after the 8th child will not be permitted.

Lazy loading disabled: Lazy loading is disabled for the current model and all referenced Glass models.

Lazy loading Only reference: Lazy loading is disabled for the current model but all referenced model will be lazy loaded.


YOU MAY ALSO LIKE