Saturday, July 16, 2016

How to Read all the fields from sitecore item programmatically






You are worried because you have added all the fields in the template and it’s coming in the item correctly but it’s not coming in the field collection with the below code implementation:

foreach (Field field in Item.Fields)
  //field value
}


Also when you call the item field explicit using below code, its coming correctly, then why it’s not coming in the field collection. Yes you are right everything is right except your field value is blank or still set as the standard value only.

string fieldvalue  = Sitecore.Context.Item["fieldName"];

Yes if the sitecore item field value is blank or set as standard value then it’s not fully instantiated in the field collection by default. So what is the solution??


No worries just one line code will resolve your problem; yes put this below line of code before calling the field collection code as below:

Item item = Sitecore.Context.Item;
item.Fields.ReadAll();
foreach (Field field in Item.Fields)
  //field value
}

Yuppy..Now all field is coming in the field collection(including the blank value field)

Hope this article will help you.

Happy sitecoring…