5

I have an existing template, Template E. This template inherits a base template, Template B. Template B has two checkbox fields. In Template E's standard value, I have checked the inherited checkbox fields and published the same. Now all the existing items referring to Template E has those two checkbox fields checked. I have a custom tracking pipeline <mvc.requestEnd> which checks this checkbox fields. Now the problem is the Context.Item is not picking the standard value fields and I am unable to see the CheckboxOne field in Context.Item.Fields.

Sample config:

<sitecore>
    <pipelines>
      <mvc.requestEnd>
        <processor type="Myproject.GoogleAnalytics, MyProject"/>
      </mvc.requestEnd>
    </pipelines>
  </sitecore>

Sample code:

namespace MyProject
{
    public class GoogleAnalytics : MvcPipelineProcessor<RequestEndArgs>
    {
        public override void Process(RequestEndArgs args)
        {
            try
            {
                if (Context.Item != null && Context.Item.Paths.IsContentItem)
                {
                    if (Context.Item.Fields[BaseConstants.CheckboxOneFieldId] != null && 
                        Context.Item.Fields[BaseConstants.CheckboxOneFieldId].HasValue && 
                            Context.Item.Fields[BaseConstants.CheckboxOneFieldId].Value.Equals("1"))
                    {
                       //Logic to implement
                        
                    }
                }
            }
            catch(Exception ex)
            {
                
            }
        }
    }
}
 

Is this a Sitecore bug? I am using Sitecore 8.2.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Richard Seal
    Oct 18, 2022 at 18:13

1 Answer 1

2

For performance reasons, Sitecore does not explicitly read item fields that have no value, or fields that have only Standard Value defined, that is why you may not see them in the collection Context.Item.Fields.

You can use one of the following options to read the required field:

  • change your code to Context.Item[BaseConstants.CheckboxOneFieldId]
  • or call the method Context.Item.Fields.ReadAll() before accessing the collection of fields

You can find more details about this behaviour in the post When is item.Fields.ReadAll() required to be run?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.