When is item.Fields.ReadAll() required to be run?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












When iterating an item's fields', sometimes the API fails to see a field unless item.Fields.ReadAll() is executed prior to checking for field existence.



I've ran into this multiple times in all my years, and running item.Fields.ReadAll() always saves the day but I don't have a clear explanation for why it returns fields sometimes, but not others.



In my most recent example, I iterated a list of items with the same template. The items without an image set (I believe this happens on other field types as well) would return as if the field didn't exist on the template.



if (product.Fields.Select(i => i.Name).All(i => i != field.Name))
continue;


The line above would trip the continue on products without an image set.



I'm aware that item[field.Name] would always return the value of the field regardless of whether or not item.Fields.ReadAll() was executed prior.










share|improve this question





















  • This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
    – Dan Sinclair
    3 hours ago














up vote
3
down vote

favorite












When iterating an item's fields', sometimes the API fails to see a field unless item.Fields.ReadAll() is executed prior to checking for field existence.



I've ran into this multiple times in all my years, and running item.Fields.ReadAll() always saves the day but I don't have a clear explanation for why it returns fields sometimes, but not others.



In my most recent example, I iterated a list of items with the same template. The items without an image set (I believe this happens on other field types as well) would return as if the field didn't exist on the template.



if (product.Fields.Select(i => i.Name).All(i => i != field.Name))
continue;


The line above would trip the continue on products without an image set.



I'm aware that item[field.Name] would always return the value of the field regardless of whether or not item.Fields.ReadAll() was executed prior.










share|improve this question





















  • This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
    – Dan Sinclair
    3 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











When iterating an item's fields', sometimes the API fails to see a field unless item.Fields.ReadAll() is executed prior to checking for field existence.



I've ran into this multiple times in all my years, and running item.Fields.ReadAll() always saves the day but I don't have a clear explanation for why it returns fields sometimes, but not others.



In my most recent example, I iterated a list of items with the same template. The items without an image set (I believe this happens on other field types as well) would return as if the field didn't exist on the template.



if (product.Fields.Select(i => i.Name).All(i => i != field.Name))
continue;


The line above would trip the continue on products without an image set.



I'm aware that item[field.Name] would always return the value of the field regardless of whether or not item.Fields.ReadAll() was executed prior.










share|improve this question













When iterating an item's fields', sometimes the API fails to see a field unless item.Fields.ReadAll() is executed prior to checking for field existence.



I've ran into this multiple times in all my years, and running item.Fields.ReadAll() always saves the day but I don't have a clear explanation for why it returns fields sometimes, but not others.



In my most recent example, I iterated a list of items with the same template. The items without an image set (I believe this happens on other field types as well) would return as if the field didn't exist on the template.



if (product.Fields.Select(i => i.Name).All(i => i != field.Name))
continue;


The line above would trip the continue on products without an image set.



I'm aware that item[field.Name] would always return the value of the field regardless of whether or not item.Fields.ReadAll() was executed prior.







fieldtype






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









jrap

1,9501623




1,9501623











  • This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
    – Dan Sinclair
    3 hours ago
















  • This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
    – Dan Sinclair
    3 hours ago















This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
– Dan Sinclair
3 hours ago




This is totally unrelated to your question, but I think your code above could be a little performant by condensing it to product.Fields.All(f => f.Name != field.Name). You're welcome for the unsolicited advice! ;P
– Dan Sinclair
3 hours ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










When you need all fields :-)



For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll is when you plan to loop through the FieldCollection - pretty much like the scenario you describe above.



For reference:




For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:




foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)

// here you will see only fields with values (even empty string) set on item level
// fields with null in them or standard values will not be here.



Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields



Reading the template definition



If you are interested in what fields are defined for your item (template), TemplateManager also has a method to help you out.



Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
TemplateField allFields = template.GetFields(true);


Adding (true) to the GetFields call, will instruct TemplateManager to also return base fields.



Essentially this is what ReadAllFields does, behind the scenes.



Source: Getting all the fields of a template in Sitecore



On source




Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item,
invoke the Sitecore.Data.Items.Item.ReadAll() method. For example, to iterate the fields of the
context item:




Sitecore.Data.Items.Item item = Sitecore.Context.Item;
item.Fields.ReadAll();
foreach (Sitecore.Data.Fields.Field field in item.Fields)

// TODO: process field



From the: Sitecore CMS 6.4 and later Content API Cookbook



Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "664"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f14594%2fwhen-is-item-fields-readall-required-to-be-run%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    When you need all fields :-)



    For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll is when you plan to loop through the FieldCollection - pretty much like the scenario you describe above.



    For reference:




    For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:




    foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)

    // here you will see only fields with values (even empty string) set on item level
    // fields with null in them or standard values will not be here.



    Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields



    Reading the template definition



    If you are interested in what fields are defined for your item (template), TemplateManager also has a method to help you out.



    Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
    TemplateField allFields = template.GetFields(true);


    Adding (true) to the GetFields call, will instruct TemplateManager to also return base fields.



    Essentially this is what ReadAllFields does, behind the scenes.



    Source: Getting all the fields of a template in Sitecore



    On source




    Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item,
    invoke the Sitecore.Data.Items.Item.ReadAll() method. For example, to iterate the fields of the
    context item:




    Sitecore.Data.Items.Item item = Sitecore.Context.Item;
    item.Fields.ReadAll();
    foreach (Sitecore.Data.Fields.Field field in item.Fields)

    // TODO: process field



    From the: Sitecore CMS 6.4 and later Content API Cookbook



    Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.






    share|improve this answer


























      up vote
      4
      down vote



      accepted










      When you need all fields :-)



      For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll is when you plan to loop through the FieldCollection - pretty much like the scenario you describe above.



      For reference:




      For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:




      foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)

      // here you will see only fields with values (even empty string) set on item level
      // fields with null in them or standard values will not be here.



      Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields



      Reading the template definition



      If you are interested in what fields are defined for your item (template), TemplateManager also has a method to help you out.



      Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
      TemplateField allFields = template.GetFields(true);


      Adding (true) to the GetFields call, will instruct TemplateManager to also return base fields.



      Essentially this is what ReadAllFields does, behind the scenes.



      Source: Getting all the fields of a template in Sitecore



      On source




      Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item,
      invoke the Sitecore.Data.Items.Item.ReadAll() method. For example, to iterate the fields of the
      context item:




      Sitecore.Data.Items.Item item = Sitecore.Context.Item;
      item.Fields.ReadAll();
      foreach (Sitecore.Data.Fields.Field field in item.Fields)

      // TODO: process field



      From the: Sitecore CMS 6.4 and later Content API Cookbook



      Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.






      share|improve this answer
























        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        When you need all fields :-)



        For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll is when you plan to loop through the FieldCollection - pretty much like the scenario you describe above.



        For reference:




        For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:




        foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)

        // here you will see only fields with values (even empty string) set on item level
        // fields with null in them or standard values will not be here.



        Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields



        Reading the template definition



        If you are interested in what fields are defined for your item (template), TemplateManager also has a method to help you out.



        Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
        TemplateField allFields = template.GetFields(true);


        Adding (true) to the GetFields call, will instruct TemplateManager to also return base fields.



        Essentially this is what ReadAllFields does, behind the scenes.



        Source: Getting all the fields of a template in Sitecore



        On source




        Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item,
        invoke the Sitecore.Data.Items.Item.ReadAll() method. For example, to iterate the fields of the
        context item:




        Sitecore.Data.Items.Item item = Sitecore.Context.Item;
        item.Fields.ReadAll();
        foreach (Sitecore.Data.Fields.Field field in item.Fields)

        // TODO: process field



        From the: Sitecore CMS 6.4 and later Content API Cookbook



        Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.






        share|improve this answer














        When you need all fields :-)



        For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll is when you plan to loop through the FieldCollection - pretty much like the scenario you describe above.



        For reference:




        For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:




        foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)

        // here you will see only fields with values (even empty string) set on item level
        // fields with null in them or standard values will not be here.



        Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields



        Reading the template definition



        If you are interested in what fields are defined for your item (template), TemplateManager also has a method to help you out.



        Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
        TemplateField allFields = template.GetFields(true);


        Adding (true) to the GetFields call, will instruct TemplateManager to also return base fields.



        Essentially this is what ReadAllFields does, behind the scenes.



        Source: Getting all the fields of a template in Sitecore



        On source




        Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item,
        invoke the Sitecore.Data.Items.Item.ReadAll() method. For example, to iterate the fields of the
        context item:




        Sitecore.Data.Items.Item item = Sitecore.Context.Item;
        item.Fields.ReadAll();
        foreach (Sitecore.Data.Fields.Field field in item.Fields)

        // TODO: process field



        From the: Sitecore CMS 6.4 and later Content API Cookbook



        Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 4 hours ago

























        answered 4 hours ago









        Mark Cassidy♦

        15.8k43178




        15.8k43178



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f14594%2fwhen-is-item-fields-readall-required-to-be-run%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery