I'm not super familiar with javascript and how it interacts with SharePoint, but I was tasked with starting a workflow on several items with one click. I did some research and found this which walked me through everything step by step.
I ended up with this code and have added the two jquery files into the SharePoint site.
<script type="text/javascript" src="...JS/StartWorkflow/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="...JS/StartWorkflow/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
function StartWFOnSelectedItems()
{
var listName = "NewArrivalInformation";
var ctx = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
var url = document.URL;
var item;
for (item in selectedItems)
{
var itemId = selectedItems[item].id;
url = url.split(listName)[0];
url = url+listName+'/'+itemId+'_.000';
StartWorkflow(url,itemId);
// alert("Workflow initiated");
}
alert("Workflow started on selected items");
}
function StartWorkflow(ItemURL,ItemID)
{
$().SPServices({
operation: "StartWorkflow",
item: ItemURL,
templateId: "{6eb308a5-46e3-4673-8b20-e3c7c1d37658}",
workflowParameters: "<root />",
completefunc: function()
{
alert(ItemID);
}
});
}
</script>
Obviously, I usually have the full links at the top, but I've taken the first part of it out for privacy reasons. I believe I linked everything in my site correctly, because when I click the button I've added, I get the alerts listed within the script and within both of the functions, meaning they're being triggered. However, the workflow doesn't run. I have checked multiple times that the template ID is correct, and I'm at a loss as to where this could be going wrong. The workflow runs when I trigger it the normal way (through the workflows button), but running it using this script does not work.