In our attempts to move more of our code into Asynchronous web services for Big History, we struggled with keeping access to
in order to make calls to the Sitecore database. We tried a number of methods first to just not use the Context.Database at all. First, we just called the database using the GetDatabase method, then fired our DB call referencing that DB. Defining a constant somewhere and
var database = Sitecore.Configuration.Factory.GetDatabase("master");
Item unit = database.GetItem(unitId);
This worked for a while, until we tied some of our larger chunks of code into web services and were looking at some pretty serious refactoring to get everything setup using the method above. Looking for a solution that actually allowed us to use Sitecore.Context.Database, I figured out our hangup had something to do with the usual Sitecore rendering pipeline not firing and populating all of the Context.
If your approach to Web Services is anything like ours, you had your Service URLs in the
pipeline in your Web.config. Your config should look something like this.
<processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
<param desc="Allowed extensions (comma separated)">aspx, ashx, asmx</param>
<param desc="Blocked extensions (comma separated)">*</param>
<param desc="Blocked extensions that stream files (comma separated)">*</param>
<param desc="Blocked extensions that do not stream files (comma separated)"></param>
Once all that is changed, you should have access to the entire Context object just like you would in any other Layout or Sublayout. One hangup to be aware of, is that if your service doesnt live inside a folder that is defined as a site in your Config, you’ll have to either create a new Site for it in your config, or just move the folder. For instance, if your site definition looked like
<site name="localhost" notFound="" hostName="localhost" virtualFolder="/" physicalFolder="/MYWEBSITE" rootPath="/sitecore/content" startItem="/Item" database="master" domain="extranet" allowDebug="true" cacheHtml="false" htmlCacheSize="10MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" patch:before="site[@name='website']" />
and your service didnt live inside the “MYWEBSITE” folder, you’ll need to move your service, or add another site that points at the folder containing your service.