AnsweredAssumed Answered

Start Workflow for selected List Items

Question asked by christian.debiasi on Jun 2, 2016
Latest reply on Jun 2, 2016 by christian.debiasi

Hi there,

 

i currently have a little problem with one of my projects. Here's what i try to do. I want to be able to select some List items and then run a function that starts a specific work flow for this itme. To accomplish this I more or less used the code if found on c-sharpcorner.com Unfortunately i always get an error message stating:

 

Failed to load subscription

Error: Ungültige Anforderung. undefined (translated: invalid request)

 

I'm not completely sure if i get the subscriptionID right which might be a point (Link looks like /StartWorkflow.aspx?List={4ccda439-0067-47f8-a452-30d4f4d2c173}&ID=2&ItemGuid={E8C7E0FA-DCAB-4475-9994-FE9BD9566D96}&TemplateID={710e22fa-2ffc-4f0b-bf47-9da7f74aff00} so i guessed the first id {4ccda... must be the right one even though i tried the function with all of them)

 

If its not the ID I'm a little lost at the moment. It would be great if someone came up with an idea. Thanks in advance and

 

Greetings Chris

 

websiteWorkflows = (function() {  


    var self = this;  
    self.Context = null,  
    self.WFManager = null;  
  
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", function() {  
        SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));  
        SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager",  
  
        function() {  
            self.Context = SP.ClientContext.get_current();  
            var web = self.Context.get_web();  
            self.WFManager = SP.WorkflowServices.WorkflowServicesManager.newObject(self.Context, web);  
        });  
    });  
  
    StartListWorkflowOnSelectedItems = function(subID) {  
        var selectedItems = SP.ListOperation.Selection.getSelectedItems(self.Context);  
  
        for (item in selectedItems) {  
            var itemId = selectedItems[item].id;  
            self.StartListWorkflow(itemId, subID);  
        }  
    };  
  
    StartListWorkflow = function(itemID, subID) {  
        var subscription = self.WFManager.getWorkflowSubscriptionService().getSubscription(subID);  
  
        self.Context.load(subscription);  
        self.Context.executeQueryAsync(  
  
        function(sender, args) {  
            var inputParameters = {};  
  
            self.WFManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemID, inputParameters);  
  
            self.Context.executeQueryAsync(  
  
            function(sender, args) {  
                var message = "The workflow " + subscription.get_name() + " was started on item with ID " + itemID;  
                SP.UI.Notify.addNotification(message, false);  
            },  
  
            function(sender, args) {  
                var message = "Failed to start workflow " + subscription.get_name() + " on item with ID " + itemID;  
                SP.UI.Notify.addNotification(message, false);  
                console.log("Failed to start workflow.");  
                console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());  
            });  
        },  
  
        function(sender, args) {  
            var message = "Failed to load subscription " + subID;  
            SP.UI.Notify.addNotification(message, false);  
            console.log("Failed to load subscription.");  
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());  
        });  
  
    };  
  
    return {  
        StartListWorkflow: StartListWorkflow,  
        StartListWorkflowOnSelectedItems: StartListWorkflowOnSelectedItems  
    };  
})(); 


Outcomes