Using Glass Mapper Item/Datasource children getting null
Published: 22 September 2022

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
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:

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.

Mitesh Patel - Technical Head - ADDACT
Sitecore || XMCloud || OrderCloud Certified
Mitesh, a distinguished Technical Head at Addact/Addxp, is a prominent figure in Sitecore/XMCloud/OrderCloud certified writing. From Sitecore XM Cloud Developer Certification to Sitecore 10 .NET Developer Certification and Sitecore OrderCloud Certification, Mitesh's expertise is unparalleled. Mitesh is not only a skilled Sitecore CMS developer but also a 12+ years experienced software engineer proficient in various technologies such as MVC, ASP.Net, C#, jQuery, and Azure cloud/AWS.

