I'm working on a project where I have a controller with a few endpoints which can be called from sitecore and non-sitecore sites. This works fine. Now I have to add a different endpoint which needs to read an item from the context database in order to return the requested data. My problem is that when I try to access the specified item I get an exception due to the database being null.

I know that the controller is not in the Sitecore context but I don't know how to solve/work around it.

Below you will find a short snippet of the controller.

public class TestApiController : Controller
    public TestApiController()
        // some stuff

    public async Task<string> SpecialAction(string data)
        string itemID = "{443FA245-623A-4EEF-B519-281A9C709B7C}";
        Item specialItem = Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(itemID));
        if (specialItem.HasChildren)
            // logic
        return response;

Try to get database using Sitecore Configuration Factory and get the item from that database-

Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
masterDB.GetItem(new Sitecore.Data.ID(itemID))
  • Although this threw a could not find configuration node databases/database @id='master' error it got me thinking. If I call the endpoint from a Sitecore page it works fine but not from within my test or other applications which have no connection to Sitecore. But this is OK for now. Thanks for your help! – Flo Sep 2 '20 at 6:10
  • are you testing this on CD? Then pass the database accordingly like web – Gaurav Agarwal Sep 2 '20 at 7:38

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.