12

I'm trying to get the Sitecore context using:

var context = new SitecoreContentContext();
var item = context.GetItem<Sitecore.Data.Items.Item>("/sitecore/content/home");

But the item assignment throws a Null Exception Error. When I debug the context variable, the property Database is null. If I set that property to "master" on debug mode, it works. But I don't know why that property null.

enter image description here

Does any one have any advice?

Thanks

  • 3
    What is the execution context of your code? Are you inside of a controller? Do you have Sitecore.Context.ContentDatabase available at that time? – Dmytro Shevchenko Oct 13 '16 at 18:21
  • 2
    As @DmytroShevchenko asked, the execution context of your code is very important. Exactly where is this being run? You may want to consider using the following switcher if it's being run in an abnormal way (through a job or even a SPEAK application): Sitecore.Data.DatabaseSwitcher – Justin Laster Oct 13 '16 at 19:26
  • @gunarb Please consider marking the answer that helped you the most as accepted. – Dmytro Shevchenko Oct 18 '16 at 13:57
9

Is you want to use the database represented by Sitecore.Context.Database then you should use SitecoreContext.

The service SitecoreContentContext is a wrapper for the database Sitecore.Context.ContentDatabase.

If the internal database used by the Glass service is null, you should check the appropriate static property.

If you want to wrap a specific database you should use SitecoreService e.g: new SitecoreService("master").

If you want to just grab a Sitecore Item, as in this code:

var item = context.GetItem<Sitecore.Data.Items.Item>("/sitecore/content/home");

Then you are probably better just using the Sitecore Database itself. Use Glass when you are working with models and not raw Sitecore items.

3

This error happens when your code is in an environment that doesn't have a Sitecore context or doesn't have a content database assigned.

To solve this,

  • Either move your code to a Sitecore-powered environment (such an MVC controller);
  • Or register the way your code executes in Sitecore (e.g. if it's an .svc service, add svc to the list of allowed extensions);
  • Or pass in the content database directly:
var context = new SitecoreContentContext(
    Factory.GetDatabase("master"),
    AbstractSitecoreContext.GetContextFromSite())
1

Check your item to make sure a version exists in the language you are opening the site in.

Also make sure the item is published and is in the web database. The fact that you see it in the debug mode shows that its in master but might not be in web.

1

You can fill up your database variable using the following statement:

Sitecore.Data.DatabasemasterDB = Sitecore.Configuration.Factory.GetDatabase("master");

But i would recommend trying to sort it out like this:

Item item1 = Sitecore.Context.Database.GetItem("/sitecore/content/home");

It will then retrieve the designated database out of its context instead of "forcing" to use a specific database. Let e know if this worked out for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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