Using GlassCast for Sitecore Items

Posted by Bhavik Patel on 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: http://www.glass.lu/en/Mapper/Sc/Tutorials/Tutorial9.aspx.

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!



CONTACT SYMSOFT SOLUTIONS

Looking for a partner who will exceed your expectations? Get in touch.

Name
*
Message
*
SYMSOFT SOLUTIONS 4090 Truxel Road, Suite 200 Sacramento, CA 95834
United States (916) 567-1740