Skip to main content
More comprehensive answer / code
Source Link
Marcel Gruber
  • 2.3k
  • 1
  • 18
  • 38

What you're asking is how to establish context inside of an API controller. They are different than regular controllers in that they don't have context. You have to be a bit cheeky about establishing it on your own and being careful while using any data that gets sent along with the GET or POST request that called the method.

This should give you some ideas:

[HttpGet]
public IHttpActionResult FeaturedCategories(string id)
{
  if(string.IsNullOrWhitespace(id))
  {
    // return some kind of error
  }

  SiteContext targetSiteContext = SiteContext.GetSite("websiteName"); // TODO: hardcoded site name
  if (targetSiteContext == null)
  {
      throw new Exception("Unable to fetch site context.");
  }

  var db = Database.GetDatabase("web");
  if (db == null)
  {
      throw new Exception("Unable to fetch database.");
  }

  using (new DatabaseSwitcher(db))
  using (new SiteContextSwitcher(targetSiteContext))
  {
    // Make calls to the HttpRequestMessage if needed

    var foo = _yourSitecoreService.GetItem("TODO"id);

    // Do some validation needed
  }
}

What you're asking is how to establish context inside of an API controller. They are different than regular controllers in that they don't have context. You have to be a bit cheeky about establishing it on your own and being careful while using any data that gets sent along with the GET or POST request that called the method.

This should give you some ideas:

SiteContext targetSiteContext = SiteContext.GetSite("websiteName"); // TODO: hardcoded site name
if (targetSiteContext == null)
{
    throw new Exception("Unable to fetch site context.");
}

var db = Database.GetDatabase("web");
if (db == null)
{
    throw new Exception("Unable to fetch database.");
}

using (new DatabaseSwitcher(db))
using (new SiteContextSwitcher(targetSiteContext))
{
  // Make calls to the HttpRequestMessage if needed

  var foo = _yourSitecoreService.GetItem("TODO");
}

What you're asking is how to establish context inside of an API controller. They are different than regular controllers in that they don't have context. You have to be a bit cheeky about establishing it on your own and being careful while using any data that gets sent along with the GET or POST request that called the method.

This should give you some ideas:

[HttpGet]
public IHttpActionResult FeaturedCategories(string id)
{
  if(string.IsNullOrWhitespace(id))
  {
    // return some kind of error
  }

  SiteContext targetSiteContext = SiteContext.GetSite("websiteName"); // TODO: hardcoded site name
  if (targetSiteContext == null)
  {
      throw new Exception("Unable to fetch site context.");
  }

  var db = Database.GetDatabase("web");
  if (db == null)
  {
      throw new Exception("Unable to fetch database.");
  }

  using (new DatabaseSwitcher(db))
  using (new SiteContextSwitcher(targetSiteContext))
  {
    // Make calls to the HttpRequestMessage if needed

    var foo = _yourSitecoreService.GetItem(id);

    // Do some validation needed
  }
}
Source Link
Marcel Gruber
  • 2.3k
  • 1
  • 18
  • 38

What you're asking is how to establish context inside of an API controller. They are different than regular controllers in that they don't have context. You have to be a bit cheeky about establishing it on your own and being careful while using any data that gets sent along with the GET or POST request that called the method.

This should give you some ideas:

SiteContext targetSiteContext = SiteContext.GetSite("websiteName"); // TODO: hardcoded site name
if (targetSiteContext == null)
{
    throw new Exception("Unable to fetch site context.");
}

var db = Database.GetDatabase("web");
if (db == null)
{
    throw new Exception("Unable to fetch database.");
}

using (new DatabaseSwitcher(db))
using (new SiteContextSwitcher(targetSiteContext))
{
  // Make calls to the HttpRequestMessage if needed

  var foo = _yourSitecoreService.GetItem("TODO");
}