Traverse all fields for Sitecore item without loading all values

When working with clones in Sitecore you sometimes need to call ReadAll() to get hold of every field. This gives the penalty of loading all the items field values one by one.

Here is an extensionmethod to get all TemplateFields for the item.

    using Sitecore.Data.Items;
    using Sitecore.Data.Managers;
    using Sitecore.Data.Templates;

    public static class SitecoreItemExtensions
    {
        public static TemplateField[] GetTemplateFields(this Item item)
        {
            Template template = TemplateManager.GetTemplate(item);
            if (template == null)
            {
                return new TemplateField[] { };
            }

            return template.GetFields();
        }
    }

Then just loop through this collection and lazy load only the fields you want, like this:

    foreach (var field in Sitecore.Context.Item.GetTemplateFields())
    {
        // logic goes here
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>