When you need to start a timer job in SharePoint you can do it from Central Administration with a cool web interface.
There, you can see all timers Jobs in your farm and control their status and logs. But, sometimes we need to start a timer job from a Sharepoint front-end interface. I mean, a simple user wants to start a process that runs in background.
Yes, we have a timer job as a class, you can instantiate and call its RunNow method for starting the process. But, we need to do something else before that.
For security reasons, users can’t start timer jobs in front-end interface. In fact, you must write this Power Shell sentence in the application server or write in your feature activation method in feature receiver class.
PowerShell script
$snap = Get-PSSnapin | Where-Object { $_.Name -eq “Microsoft.SharePoint.Powershell” }
if ($snap -eq $null)
{
Add-PSSnapin “Microsoft.SharePoint.Powershell”
}
# get content web service
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
# turn off remote administration security
$contentService.RemoteAdministratorAccessDenied = $false
# update the web service
$contentService.Update()
C# code
// SET THE REMOTE ADMINISTATOR ACCESS DENIED FALSE
SPWebService.ContentService.RemoteAdministratorAccessDenied = false;
SPWebService.ContentService.Update();
Remeber that any timer job in Sharepoint is started by SP Timer and you need to restart that service in all farm servers when any change in your custom job are made.
Without modifing RemoteAdministrationAccessDenied to false when you try to start a job programatically a Access denied exception is thrown.
More information:
Custom SPJobDefinition and “Access denied” Error
Custom job and System.Security.SecurityException: Access Denied