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

Sitecore Technical Architect

Bhavik Patel is a certified Sitecore developer who has held Sitecore Most Valuable Professional (MVP) status since 2015. He has more than a decade of experience with enterprise-class Sitecore and Microsoft .NET framework development, including Sitecore integration with SharePoint and Business Intelligence solutions. He leads our Sitecore development practice, engineering enterprise-class Sitecore implementations for Government and commercial sector clients. He's an expert at using Sitecore Helix Framework to create re-usable architectures, streamline the integration of front-end development and provide the foundation for SEO-optimized Sitecore marketing websites.

Explore topics:

Other Blog Posts from Bhavik Patel