Using GlassCast for Sitecore Items

Bhavik Patel • 11/28/2014

One very useful Glass Mapper method is called GlassCast. This method is essentially used to convert a regular Sitecore.Data.Items.Item into an Glass Mapper object that you have created. It's really handy when you have issued queries in your code for certain Sitecore items and want to convert them to your Glass Mapper objects in order to reference the item's properties. You can find the official tutorial on how to use GlassCast on the Glass Mapper website:

It basically says: "The GlassCast method allows you to convert a Sitecore item to a specific type loaded by the default Glass.Mapper context. Data is read directly from the item avoiding any unnecessary calls to the Sitecore data layer".

So, for example, if you have queried for and received a Sitecore.Data.Items.Item that represents an Event data template that you have created a Glass Mapper class for, the following statement will cast that item to your Event class:

var glassMapperEvent = item.GlassCast<YourNamespace.Models.Event>();

Very useful!

Bhavik Patel

Bhavik is a .NET developer with over 9 years of experience, focusing on Sitecore CMS for the last 3 years. He has worked on several Sitecore CMS projects in both single-site and multi-site scenarios, and truly enjoys how extensible it is. He has a passion for problem solving and using technology to create engaging websites and applications. Bhavik is a Sitecore Technology MVP, 2015 and 2016.

Explore topics:

Other Blog Posts from Bhavik Patel

Subscribe for insights, trends and big ideas