Issue with getting database via Sitecore API

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s