Skip to main content

I'm working on a project where I have a controller with a few endpoints which can be called from sitecoreSitecore and non-sitecoreSitecore sites. This works fine. Now I have to add a different endpoint whichthat 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.

[RoutePrefix("api/test")]
[HandleException]
public class TestApiController : Controller
{
    public TestApiController()
    {
        // some stuff
    }

    [HttpPost]
    [Route("SpecialAction")]
    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;
    }
}

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.

[RoutePrefix("api/test")]
[HandleException]
public class TestApiController : Controller
{
    public TestApiController()
    {
        // some stuff
    }

    [HttpPost]
    [Route("SpecialAction")]
    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;
    }
}

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 that 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.

[RoutePrefix("api/test")]
[HandleException]
public class TestApiController : Controller
{
    public TestApiController()
    {
        // some stuff
    }

    [HttpPost]
    [Route("SpecialAction")]
    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;
    }
}
Source Link
Flo
  • 45
  • 4

Accessing Sitecore Context in a normal API

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.

[RoutePrefix("api/test")]
[HandleException]
public class TestApiController : Controller
{
    public TestApiController()
    {
        // some stuff
    }

    [HttpPost]
    [Route("SpecialAction")]
    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;
    }
}