SpaceJump
SpaceJump

Reputation: 483

Sitecore Scheduled Task: Cannot access any items in run class

I configured a scheduled task via database, which is triggered as expected and calls a method "Execute" in a C# class. My problem is that in my Execute method no Sitecore items can be accessed. I even get a NullPointerExecption when calling Sitecore.Context.Database.

I guess this must be a security problem, which makes sense. So I tried to get the permissions using UserSwitcher and SecurityDisabler, but that did not help.

Can anyone point me to what I'm missing?

Upvotes: 2

Views: 1186

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27152

You cannot use Sitecore.Context in a scheduled task. It does not exist there. Sitecore.Context is tied with HttpRequest, and there is no request in a scheduled task. And as John W writes in his article (The Sitecore Context):

The Sitecore context, exposed by the static Sitecore.Context class, contains information about the Sitecore installation and the current HTTP request. Processors in the httpRequestBegin pipeline defined in the web.config file are largely responsible for defining the Sitecore context. After Sitecore determines the context database, it determines the context item in that database.

In a schedule job you should get the database by its name, e.g.:

var masterDb = Sitecore.Data.Database.GetDatabase("master");
var webDb = Sitecore.Data.Database.GetDatabase("web");

Upvotes: 6

Related Questions