Intro
Sometimes, we get the requirement to start a SharePoint workflow from JavaScript code or have to rerun a work on all existing list items and want to give an easy option to users to trigger a workflow on all items, then this the solution will help you.
We’re basically triggering a workflow from JavaScript code and you have the option to rerun workflow on all items or on a few items based on your requirement.
Prerequisites
We need SPServices JS file reference to use this solution and I think we can use SharePoint REST API as well to trigger a workflow. But you need to update code a little bit and REST API endpoints may not work with SP2010.
Steps
Save the below code in a text file by updating the global variable to point out to the List, Workflow name, and Workflow Template ID.
I selected all items to trigger workflow but if you have to filter the result then update query in REST call. I used logging to make sure the code runs correctly. You may discard the console logging if you want.
Update the HTML code block as I have used <span> to show a message and a button to trigger a JavaScript function, you can update it as per your need.
Finally, upload this text file to the Site assets folder of your SharePoint site and refer it in the content editor web part to render the HTML and code on-page.
Code
- < !DOCTYPE html > < script > < /script> / / add the SPService webservice reference here..... < script type = "text/javascript" >
-
- var workflowName = "";
- var listName = "";
- var listGUID = "";
- var tempalteID = "";
-
- function RunWorkflow() {
- var context = SP.ClientContext.get_current();
- if (context != null) {
- $.ajax({
- async: true,
-
- url: _spPageContextInfo.webAbsoluteUrl + "/api/web/lits/getbyid(guid'" + listGUID + "')/items/?$top=1500,
-
- method: "GET",
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose"
- },
- success: function(data) {
- if (data != null || data !== "" || data !== undefined) {
- console.log("Total item count is:" + data.d.results.length);
- for (var i = 0; i < data.d.results.length; i++) {
- var item = data.d.results[i];
- console.log("workflow starting on item: " + item.ID);
-
- startWorkflow(item.ID);
- }
- }
- },
- error: function(error) {
- JSON.stringify(error);
- }
- })
- }
- }
-
- function startWorkflow(itemId) {
- var url = _spPageContextInfo.webAbsoluteUrl + "/Lists/" + listName + "/" + itemId + "_.000";
- $().SPService({
- operation: "StartWorkflow",
- item: url,
- async: true,
- temaplteId: tempalteID,
- workflowParameters: "<root />",
- completefunc: function() {
- console.log("workflow started on item :" + itemId);
- }
- });
- }
- </script>
- <body>
- <!-- you can add HtmL code here and invoke the RunWorkflow() fucntion. I used here simple button -->
- <div class="row">
- <span>
- <h2> Click on below button to run a workflow.</h2>
- </span>
- <br/>
- <br/>
- <button type="button" class="btn btn-primary" onclick="RunWorkflow()"></button>
- </div>
- </body>undefined</html>
Conclusion
We can leverage client-side code to trigger the SharePoint workflow and even update the current context. We can trigger the workflow out of SharePoint.