Tuesday, June 15, 2010

Execadmsvcjobs alternative in SharePoint 2010. Run job immediately

In sharePoint 2007, whenever we needed to ensure that any pending jobs are not there we were used to run the following command. One scenario where we needed this command is when you have deployed a solution to a web or retract solution from web application. In these case, SharePoint doesn’t deploy or retract solution immediately rather creates jobs. So if want to run any command that depends on solution deployment then you need to run the following command to make sure there is no pending jobs.

stsadm –o execadmsvcjobs

However in SharePoint 2010, stsadm is kind of dead. So if you now need to deploy a solution and then activate feature, then after running solution deployment command you need to make sure that the solution deployment job is executed before activating/installing feature. I have found a concept in the link: http://msdn.microsoft.com/en-us/library/ff459292.aspx. I have modified the code a bit to generate the following PowerShell function. Passing a solution name in the function, check if the solution job is finished or not. If not finished then the function waits for the solution deployment job to finish.

function WaitForJobToFinish([string]$SolutionFileName)
{ 
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job.."
    }
}

In my another blog http://ranaictiu-technicalblog.blogspot.com/2010/05/sharepoint-2010-deployment-powershell.html I have described the in details on how to use the function.

2 comments:

  1. very nice post. our company has providing SharePoint development and SharePoint Consulting. Sharepoint Development

    ReplyDelete

Note: Only a member of this blog may post a comment.