来自 SPWeb 的计时器作业
我想从代码中启动计时器作业需要农场管理员凭据。但是,我需要从将在任何网站中使用的 Web 部件启动计时器作业。现在,当我尝试启动作业时,它显然给了我一个访问被拒绝的错误,因为应用程序池身份不是场管理员。关于如何解决这个问题有什么想法吗?
谢谢,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计时器作业以场管理员身份运行,并不打算由最终用户直接触发。由于某些作业可能是资源密集型的,因此只有场管理员才能创建新作业或修改现有作业的计划。
一种解决方案是使用
SPWorkItem
< /a> 用于对用户任务进行排队的基础结构,然后由派生自SPWorkItemJobDefinition
。您的 Web 部件将调用SPSite.AddWorkItem
< /a> 添加工作项。当计时器作业运行时,它将查找具有匹配 WorkItemType GUID 的任何工作项并调用ProcessWorkItem
重载。Timer jobs run as the farm administrator and are not intended to be triggered directly by an end-user. Since some jobs may be resource intensive, only the farm admin can create new jobs or modify the schedule for existing jobs.
One solution is to use the
SPWorkItem
infrastructure to queue user tasks which are then processed by a custom timer job derived fromSPWorkItemJobDefinition
. Your webpart would callSPSite.AddWorkItem
to add the work item. When your timer job runs, it will look for any work items with the matching WorkItemType GUID and invoke theProcessWorkItem
overload.你是对的。要启动计时器作业,应用程序池用户必须是场管理员。由于启动计时器作业需要您使用
SPSchedule
更新SPJobDefinition
对象。SPJobDefinition
是存储在 SharePoint 配置数据库中的SPPersistedObject
。只有场管理员才能写入此数据库。我看不出有什么办法可以解决这个问题。
解决方法:
根据您的要求,您可以编写定期运行的主作业。此作业可以查询 SharePoint 列表并启动由此类列表项定义的另一个作业。由于主作业在场管理员帐户下运行,因此该作业将能够启动新的计时器作业。
Your are right. To start a timer job the app pool user has to be farm administrator. Since starting a timer job requires you to update a
SPJobDefinition
object with anSPSchedule
. TheSPJobDefinition
is aSPPersistedObject
which is stored in the SharePoint Config Database. Only farm administrators can write into this db.I don't see a way to get pass this issue.
Workaround:
Depending on your requirements you could write a master job that runs on regular basis. This job could query a SharePoint list and start another job defined by such a list item. Since the master job runs under the Farm Administrator account, the job would be able to start a new timer job.