我使用以下代码创建了一个计时器作业:
namespace EmployeeDemo
{
class DeleteEmployees : SPJobDefinition
{
public DeleteEmployees() : base() { }
public DeleteEmployees(string jobName, SPWebApplication webapp)
: base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = "Delete Employees Timer Job";
}
public override void Execute(Guid targetInstanceId)
{
// Code
}
}
}
然后我创建了网站范围功能。在其事件接收器中,在停用时,我有以下代码:
const string timerJobName = "Delete Employees Timer Job";
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
DeleteJob(properties.Feature.Parent as SPSite);
}
private void DeleteJob(SPSite site)
{
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == timerJobName)
{
job.Delete();
}
}
}
但是它让我错误。从调试我明白错误是在行job.Delete();
上抛出的。错误是:
对象引用未设置为对象的实例。
这让我感到困惑,因为语句job.Name
执行完美,甚至让我得到了正确的计时器作业对象。但在尝试删除作业时,它会引发错误。
我发现this question与我的问题类似,但这些建议对我不起作用。从this discussion开始,我尝试在SPSecurity.RunWithElevatedPrivileges
中编写删除代码,但它仍然不适用于我。
任何人都知道为什么会发生这种情况以及如何解决这个问题?
更新1
更多细节。
NullReferenceException未被用户代码
处理对象引用未设置为对象的实例。
这里是堆栈跟踪
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.DeleteObject(Guid id)
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.Microsoft.SharePoint.Administration.ISPPersistedStoreProvider.DeleteObject(SPPersistedObject persistedObject)
at Microsoft.SharePoint.Administration.SPPersistedObject.Delete()
at Microsoft.SharePoint.Administration.SPJobDefinition.Delete()
at EmployeeDemo.Features.DeleteEmpFeature.DeleteEmpFeatureEventReceiver.DeleteJob(SPSite site)
at EmployeeDemo.Features.DeleteEmpFeature.DeleteEmpFeatureEventReceiver.FeatureDeactivating(SPFeatureReceiverProperties properties)
at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)
答案 0 :(得分:0)
要删除作业,请使用powershell:
$jobToDelete = Get-SPTimerJob -WebApplication "http://intranet" -Identity "YourJobName"
$jobToDelete.Delete()
然后运行:
iisreset /noforce
在配置数据库中,您需要授予这些权限以允许删除工作:
use [Sharepoint_Config]
GO
GRANT EXECUTE ON [dbo].[proc_putObjectTVP] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putObject] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putDependency] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putClass] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_getNewObjects] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_dropObject] TO [WSS_Content_Application_Pools]
GO