i’ve spend the last couple hours trying to figure out what’s wrong with my code. I was trying something simple, getting an item from the Sitecore Web database. Which turns out not so simple after all, frequently i cannot retrieve any field values from the item that i retrieved.
After a long time trying out different things, renaming the section, rename the item name, change the template, change the content tree structure, checkout if any custom pipeline might be the isssue…
It turns out that this line of code -which looks ok at first glance- has an issue
// create new database instance Database database= new Database("Web"); // get item var item = database.GetItem(itemId); // get the field value var value = item.Fields["My Field"].Value;
in other area of the software that we develop we were using this code to get the database instead
Database database = Database.GetDatabase("Web");
it turns out that those two ways of getting the database instance is totally different from what i originally thought.
By using the constructor approach we only create an empty database object while using the latter approach it will build the database instance with all of the configuration values within the config file via reflection.
thanks to this Stackoverflow post my misery has ended.