0

can I kick off a SharePoint timer job from a client side JavaScript?

  • We can't but would like to understand why you wanted to fire it from a javascript? Just explaining the broader issue can help us give a nice solution. – Arvi May 31 '18 at 0:16
  • what I am trying to do is get a user create an item in a list that he only has read access to. So when this user hits submit from the browser, it will create a list item in different list that he does have write perms to and then the JavaScript will fire off a timer job which will copy the list item that the user created in the list (to which he has write access to) to the other list that he does not have access to. makes sense? its a crazy requirement but seems like that what I have to accomplish – vivek m May 31 '18 at 14:56
  • this does not sound like a crazy requirement. But using a timer job for this is an overkill. Just use a SharePoint workflow on a list A that will trigger automatically when a new item is created. This workflow will create an item in a list B and copy all fields from the source list item to the destination. No need to call anything from JavaScript – Denis Molodtsov May 31 '18 at 15:10
  • yes (we did go the route you mentioned) but we have a workflow that runs on list B that needs to kick off when new items get created in it. What we found is that the workflow on list B was not getting kicked off if the items were getting created in list B via a workflow. That's why the need to create a timer job. – vivek m May 31 '18 at 15:29
2

You can't do it directly. It is only possible using server-side object model from PowerShell or C#.

But if you really need it - we can create a "dirty workaround". I used to use this approach because I just hate creating the "proper" REST services inside SharePoint, they are a nightmare to work with and debug if something goes wrong. Instead I used to create aspx pages that return JSON.

1. Create a full trust farm solution

2. Create a Layouts page. for example /_layouts/System/JobRunner.aspx

You can send GET requests from your JavaScript by calling this page like so: /_layouts/System/JobRunner.aspx?JobName=MyCustomJobName"

Inside that page, on PageInit you can check if the current user has enough permissions to do such an activity if they do - using RunWithEvelavatedPrivileges, get the timer Job you want and just run it:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    // TODO: check if the current user can do such unsafe activity
    // TODO: Wrap this code with RunWithElevatedPriviliges. Otherwise it won't work:
    var myCustomJobName = Request.QueryString["JobName"];
    var jobToRun = (from jobDefinition in currentApp.JobDefinitions
                            where jobDefinition.DisplayName == myCustomJobName
                            select jobDefinition).SingleOrDefault();
    if (jobToRun != null)
    {
       jobToRun.RunNow();
    }

    // Return JSON here (see below for an example)
}

Then after it's done the page can let your JavaScript know that it worked just fine by returning a JSON:

string json = "{\"results\":\"success! The Timer Job is now running\"}";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();

3. Testing it with jQuery:

$.get( "/_layouts/System/JobRunner.aspx?JobName=MyCustomJobName", function(data ) {
    alert( data.results );
});
  • Thanks Denis. Sounds like your solution will do what is required. I have posted above in a comment that real requirement that's why I was thinking of calling a web service from client side JavaScript. Any comments are most welcome. I will mark this response as the answer. – vivek m May 31 '18 at 15:03
1

You can create a Windows Azure Timer Job and use CSOM (Client-side object model) as an alternative to a full thrust timer job.

Reference:

http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2014/04/07/sharepoint-timer-jobs-running-as-windows-azure-web-jobs.aspx

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.