SiteContext.GetWebSiteUrl() – Hidden use of ContentDatabase

I was recently looking at Sitecore composite components by Chris van de Steeg. It’s a great and useful concept which he explains well. I grabbed his implementation on Github to give it a try.

Unfortunately the code in CompositeComponentController threw a NullReferenceException inside a call to SiteContext.GetWebSiteUrl(). Having never used that particular method before, I turned to my primary Sitecore documentation tool – the decompiler. Turns out the method calls an overloaded version of it self by passing a Database as argument – but not just any database, Sitecore.Context.ContentDatabase.

Maybe I’m too much of a Sitecore newbie for asking this, but what is the content database in code running on a CD-server? In my solution it’s null. Decompiling into the ContentDatabase property of Sitecore.Context, I see that you can set a content database on the site definition config using a content attribute. Should I set it to web on CD-servers?

Have you experienced other Sitecore APIs that make hidden use of ContentDatabase?

Use of ContentDatabase in SiteContext.GetWebSiteUrl

Disable Sitecore filters for Web API controllers

I have previously written about how easy it to implement Web API controllers in Sitecore. However since Sitecore adds a lot of global filters for authorization and security policies – e.g. see sitecore/api/configuration/filters section in Sitecore.Services.Client.config. You might want to disable these filters, as they can interfere when building a public accessible API – see gist below. During a debugging session I noticed that a filter from some FXM assembly were also added.

To be fair, most of Sitecore’s implementations of IFilter/ActionFilterAttribute that are assigned globally on HttpConfiguration, does check for presence of ServicesControllerAttribute and inheritance of ServicesApiController. This means you might prevent these filters from doing anything by using System.Web.Http.ApiController as base class for your controllers. I haven’t been though the code for all Sitecore’s filters, so I’m playing it safe, and decorate my ApiControllers with a ClearSitecoreWebApiConfigAttribute.

I encourage the developers of Sitecore to consider using controller-specific configuration instead of loading the global HttpConfiguration with all kinds of module or feature specific stuff.