1

I am trying to invoke a SPTimerJob to run on demand. What I'm trying to do, is to call Word Automation Services to convert my word files.

So far I have tried our different solutions and none of them seems to be working.

I know, that working command would be Start-SPTimerJob "Word Automation Services". When I run that command from PowerShell it self, then it works like a charm, but when I try to call it with the following code, it seems to have no effect:

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            PowerShell ps = PowerShell.Create();
            ps.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
            ps.AddScript("Start-SPTimerJob 'Word Automation Services'");
            ps.Invoke();
         });

I appreciate all the help! :)

EDIT:

I have tried few different approaches, and I get The method or operation is not implemented exception.

example:

SPSite spSite = SPContext.Current.Site;
            foreach (var service in spSite.WebApplication.Farm.Services)
            {
                if (service.TypeName == "Word Automation Services")
                {
                    foreach (var jobDefinition in service.JobDefinitions)
                    {
                        if (jobDefinition.Name == "Word Automation Services")
                        {
                            //jobDefinition.RunNow();
                            jobDefinition.Execute(Guid.Empty);
                            break;
                        }
                    }
                }
            }

With Akhoy code:

SPService s = SPFarm.Local.Services.FirstOrDefault(s1 => s1.TypeName.Equals("Word Automation Services"));
            foreach (SPJobDefinition job in s.JobDefinitions)
            {
                if (job.DisplayName.Equals("Word Automation Services Timer Job"))
                {
                    job.Execute(jobGuid);
                }
            }

But I get following exception (tried with Guid.Empty and current Guid on the picture is from ConversionJob.JobId: notImplementedException

  • Change Execute to RunNow(). It works fine for me. Check update. – Akhoy Sep 14 '15 at 11:34
  • Currently trying out solution with RunNow(), but I have some Security issues with that System.Security.SecurityException: Access denied.. Tells me to edit my application's trust level in configuration file, not sure, why this problem occurs, but trying to figure it out. – Taurib Sep 14 '15 at 12:04
  • 1
    You need to be a farm admin to run that. – Akhoy Sep 14 '15 at 12:05
  • Doesn't SPSecurity.RunWithElevatedPrivileges(delegate(){ }); do that trick? – Taurib Sep 14 '15 at 12:15
  • 1
    No. It runs under app pool account not farm admin account. Also, see if this helps. social.msdn.microsoft.com/Forums/sharepoint/en-US/…. But if your app pool account and farm account is the same, then RWEP might work. – Akhoy Sep 14 '15 at 12:16
2

SharePoint timer service (OWSTIMER.exe) which handles the running of timer jobs runs under farm account credentials. Running it under application pool account wouldn't work.

And there's no need to use PowerShell to start a timer job if you're using C# since there are server side API's which let you do the same thing.

SPService s = SPFarm.Local.Services.FirstOrDefault(s1 => s1.TypeName.Equals("Word Automation Services"));
foreach (SPJobDefinition job in s.JobDefinitions)
                {
                    if (job.DisplayName.Equals("Word Automation Services Timer Job"))
                    {
                        job.RunNow();
                    }
                }
  • I get The method or operation is not implemented exception, when i try to execute this command. Since, I can't find "Word Automation Services", I am using "Document Conversions" instead. – Taurib Sep 14 '15 at 10:20
  • You'll have to enter the correct timer job name. For me, Word Automation Services display name is showing as "Word Automation Services Timer Job". – Akhoy Sep 14 '15 at 10:29
  • It's weird, since, when I run "Word Automation Services" under PowerShell, then it works like a charm, but I can't find that job or anything close to it under spSite.WebApplication.JobDefinitons. – Taurib Sep 14 '15 at 10:54
  • 1
    Updated code. Tested this. This seems to work fine. – Akhoy Sep 14 '15 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.