Sitecore 计划任务:无法访问 运行 class 中的任何项目
Sitecore Scheduled Task: Cannot access any items in run class
我通过数据库配置了一个计划任务,它按预期触发并调用了 C# class 中的方法 "Execute"。我的问题是在我的 Execute 方法中无法访问 Sitecore 项目。我什至在调用 Sitecore.Context.Database.
时得到一个 NullPointerExecption
我猜这一定是一个安全问题,这是有道理的。所以我尝试使用 UserSwitcher 和 SecurityDisabler 获取权限,但这没有帮助。
任何人都可以指出我所缺少的东西吗?
您不能在计划任务中使用 Sitecore.Context
。它在那里不存在。 Sitecore.Context
绑定HttpRequest,定时任务中没有请求。正如 John W 在他的文章 (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.
在计划作业中,您应该通过名称获取数据库,例如:
var masterDb = Sitecore.Data.Database.GetDatabase("master");
var webDb = Sitecore.Data.Database.GetDatabase("web");
我通过数据库配置了一个计划任务,它按预期触发并调用了 C# class 中的方法 "Execute"。我的问题是在我的 Execute 方法中无法访问 Sitecore 项目。我什至在调用 Sitecore.Context.Database.
时得到一个 NullPointerExecption我猜这一定是一个安全问题,这是有道理的。所以我尝试使用 UserSwitcher 和 SecurityDisabler 获取权限,但这没有帮助。
任何人都可以指出我所缺少的东西吗?
您不能在计划任务中使用 Sitecore.Context
。它在那里不存在。 Sitecore.Context
绑定HttpRequest,定时任务中没有请求。正如 John W 在他的文章 (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.
在计划作业中,您应该通过名称获取数据库,例如:
var masterDb = Sitecore.Data.Database.GetDatabase("master");
var webDb = Sitecore.Data.Database.GetDatabase("web");