SPContext.Current is null while calling SPContext.Current.Web.EnsureUser() method. Any suggestions?

  • What's the context of your code? Web part? Event receiver?... – Evariste Sep 18 '17 at 5:38
  • I'm trying to fetch users [author, editor) at item level using SPContext.Current.Web.EnsureUser() – raju Sep 18 '17 at 6:16
  • What's the context? A Web part, a job, a PowerShell command, ...? That's our question! – Evariste Sep 18 '17 at 7:03
  • Code is being called from ItemUpdated event – raju Sep 18 '17 at 8:42
  • Not enough: you mean a list event reveiver, right? This can be triggered in a Web context (item udpated from the UI) or not (item updated from a workflow, from PowerShell, from a job...). Please, read my answer below, as I already explained this. – Evariste Sep 18 '17 at 9:04

SPContext.Current can only be used from a Web context.

This means it works fine as long as an HttpContext exists.

By definition, this is always the case for Web parts, controls and Web pages.

For list event receivers and feature event receivers, it may or it may not be available: it depends on how the item (resp. the feature) as been added (resp. activated): from the UI (most common case: SPContext.Current is then available), from PowerShell (not available), from custom .exe (not available)...
In a feature event receiver you can safely use properties.Feature.Parent instead.
In a list event receiver, you can easily get access to the Web that originated the event with properties.Web.

For workflows, it also depends, as a workflow action may run in the Web context that triggered it (OK) or in a job (KO).

Jobs always run in a dedicated process (owstimer.exe) where SPContext.Current is never available. The concept of "contextual Web" has no signification here, since a job runs out of any user interaction, and, most of the time, simply loops through all existing sites.


SPContext.Current is not available in all scopes - it will work in Event Receivers and web service calls, but will be null in Timer Jobs and within RunWithElevatedPrivileges (as suggested here). From where are you trying to access SPContext.Current?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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