Thursday 30 January 2014

Run Timer Job in button Click event without go to Central Admin

This is mainly used to run Timer Job in button Click event without going to Central Admin


//To check whether the Timer job in currently Running or not
                            if (site.WebApplication.RunningJobs.Cast<Microsoft.SharePoint.Administration.SPRunningJob>().Any(curRunningJob => curRunningJob.JobDefinitionTitle.Equals("JobName")))
                            {
}
}
                            else
                            {

//Collect all job in SharePoint Central Admin
                                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                                {
                                    if (job.Name == "JobName")
                                    {
                                        var strStatus = job.Status;

                                        // THE ORIGINAL VALUE OF REMOTE ADMINISTRATOR
                                        var remoteAdministratorAccessDenied = SPWebService.ContentService.RemoteAdministratorAccessDenied;
                                        try
                                        {

                                            // SET THE REMOTE ADMINISTATOR ACCESS DENIED FALSE
                                            //  SPWebService.ContentService.RemoteAdministratorAccessDenied = false;

                                            if (remoteAdministratorAccessDenied == true)
                                            {
                                                SPWebService myService = SPWebService.ContentService;
                                                myService.RemoteAdministratorAccessDenied = false;
                                                myService.Update();
                                                job.RunNow();


                                            }

                                            else
                                            {

                                                job.RunNow();

                                            }


                                        }
                                        catch (Exception ex)
                                        { }

                                        finally
                                        {

                    // SET THE REMOTE ADMINISTRATOR ACCESS DENIED BACK WHAT IT WAS

                                            SPWebService.ContentService.RemoteAdministratorAccessDenied = remoteAdministratorAccessDenied;
                                        }

                                    }
                                }
                            }



Before Deploy that we fallow below steps
1. Create .ps1 file and paste Below Code into that file and Run that file
                                          (or)
Copy and paste below Code in Power shell


#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                          Configuring RemoteAdministratorAccessDenied
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
function LoadSharePointPowerShellEnvironment
{
   write-host
   write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
   write-host
   Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
   write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function SetRemoteAdministratorAccessDenied()
{
       # load sharepoint API libs
       [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
       [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") > $null

  # First get the content web service
 $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  # Now set the remote administration security to off
 $contentService.RemoteAdministratorAccessDenied = $false
  # Also update the web service
 $contentService.Update()
     
}


write-host
LoadSharePointPowerShellEnvironment

SetRemoteAdministratorAccessDenied

2. Create another .ps1 file and paste Below Code into that file and Run that file
                                          (or)
Copy and paste below Code in PowerShell

#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                          Attempting to set the status of the service instance to online
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
$farm  = Get-SPFarm
$disabledTimers = $farm.TimerService.Instances | where {$_.Status -ne "Online"}
if ($disabledTimers -ne $null)
{
    foreach ($timer in $disabledTimers)
    {
        Write-Host "Timer service instance on server " $timer.Server.Name " is not Online. Current status:" $timer.Status
        Write-Host "Attempting to set the status of the service instance to online"
        $timer.Status = [Microsoft.SharePoint.Administration.SPObjectStatus]::Online
        $timer.Update()
    }
}
else
{
    Write-Host "All Timer Service Instances in the farm are online! No problems found"

}

1 comment:

  1. This is not working for me .I want to run my timer job from wcf web service,
    Please share wheather this is possible or not.

    ReplyDelete