0

I am working on Glassmapper 5 and I have baseType and derived type. I need to get derived type(IRich_Text_Section) property values by using basetype(I_PageSection). It is working fine in glassmapper4.

public partial interface IGlassBase
{       
        [SitecoreId]
        Guid Id{ get; }

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

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

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

        [SitecoreInfo(SitecoreInfoType.Name)]
        string ScItemName { get; }

        [SitecoreItem]
        Item ScItem { get; }

        [SitecoreField("__Sortorder")]
        string SortOrder { get; }

}

    [SitecoreType(TemplateId=I_PageSectionConstants.TemplateIdString )]
    public partial interface I_PageSection : IGlassBase 
    {
    }

    [SitecoreType(TemplateId=IRich_Text_SectionConstants.TemplateIdString )]
    public partial interface IRich_Text_Section : IGlassBase , I_PageSection
    {
    
                    [SitecoreField(IRich_Text_SectionConstants.Body_TextFieldName, Setting=SitecoreFieldSettings.InferType)]
                    string Body_Text  {get; set;}
            
    }

    public ContentSectionViewModel Format(I_PageSection pageSection)
    {   
       //I am able to get item from pageSection(id, Language,version). Now i need to get Body_Text value so used the below code and getting null value in section after casting.
        **IRich_Text_Section section = pageSection as IRich_Text_Section;** 
    }

Getting null after assigning to IRich_Text_Section.

IRich_Text_Section section = pageSection as IRich_Text_Section;

Appreciated it anyone can help with this. Thanks in advance!!

1

In Glassmapper V5, lots of things are obsolete. One of the important updates is new contexts which include IMvcContext, IRequestContext, IWebFormsContext. From your question, it looks like you want to cast the pageSection type to IRich_Text_Section. You can not simply do it using the above code, You need to create an object of MVC Context (probably the one you will be working on the most), and then needs to be converted to the Sitecore model by using SitecoreService-

IMvcContext mvcContext = new MvcContext();
IRich_Text_Section section = mvcContext.SitecoreService.GetItem<IRich_Text_Section >(pageSection.Id);
1
  • Do you know the actual root cause, shouldn't it still work even though it is obsolete? Asking because I'm having similar issues after my upgrade and rewriting everything just is not an option. Any insight is much appreciated!
    – Jeroen
    Sep 28 at 17:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.