Scenario in our Sitecore 8.1 instance is that, we have a .NET FileSystemWatcher object that is initialized in Application_BeginRequest of global.asax. When a file is dropped in the watched folder, callback code interacts with Sitecore and at some point, attempts to add a new item version (via Item.Versions.AddVersion). At this point, a NullReferenceException is raised and we've traced this to a custom LinkProvider code that uses Context.Site, that turned out to be the null reference.

Is this expected behavior? Any suggestions, how we can work around this issue?

2 Answers 2


This is expected behaviour.

The reason you do not have a Context.Site (or any context for that matter) is, that you are not in a Sitecore page execution context.

The very short explanation is; Sitecore will build up the Context as part of the httpRequestBegin pipeline which is only fired - which makes sense I hope - when a HttpRequest begins.

In your case, your code is triggered by .NET in form of a FileSystemWatcher event. Not a page request. Under these circumstances, your code cannot rely on any page execution context, and must be able to execute without it.

The LinkProvider in question should fail gracefully without a context site.


As you overwrote the Global.asax this could be the reason, because Sitecore has its own. So maybe you call your custom implementation before Sitecore calls its own pipelines. To fix this try to use the httpRequestBegin pipeline. Here is the exact mapping between Global.asax methods and Sitecore pipelines (source):

  1. Application_Start: initialize
  2. Application_BeginRequest: preprocessRequest / httpRequestBegin
  3. Application_EndRequest: httpRequestEnd
  4. Session_End: sessionEnd
  5. Application_End: shutdown

Not recommended to overwrite Global.asax which is provided by Sitecore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.