Run workflow on selected list/library items

  • 1 December 2015
  • 5 replies
  • 26 views

Badge +3

I would like to create a workflow that creates an item in a separate list, that is linked (using Related Items field), to the selected documents in a Library. In essence, I want to query the library for items that are selected; build a collection of these; then loop through that collection to build the string for the related items, and update the field in the newly created item. The last bits, are very doable, it is getting the selected items in to a collection that I am a bit fuzzy on.

 

Is this recreational impossibility, or should I be able to do this?

 

Thanks.


5 replies

Userlevel 5
Badge +12

I think you could come up with something close - if you had a metadata field that you marked (ShouldRunWorkflow): (such as yes/no) and then you could query all the items with this metadata marked as yes and have the workflow query for this list and act on them collectively.   You could then have the workflow reset this value to No in case you wanted it to run again or not run again at another time.   This is just a quick thought, but might get you started in the right direction.

-Mike

Badge +3

Thanks. I think I saw something like that on another thread, but want to avoid adding a field that users would have to change.

If I were doing this, that might be OK, but this is for end users...so I cannot make them set a field, it is enough to get them to select multiple items. Even though the idea is the same, it is different enough to cause friction.

It would appear that I need to use client side script to accomplish this. Certainly makes sense, since each client may have a different selection. That also speaks to not using a field, since multiple users might be setting values at the same time.

Badge +7

Hi John,

 

You can do this with client side rendering. I've pasted a very simple version of the script you'll need below, you'll need to edit it to point at the correct workflow and list name. For simplicity I've added the item ID as the last column in the list view so I can find it easily, then just use the following script as your JS Link source:

 

(function () {
  
    (window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.10.0.min.js"></script>'));

    var viewContext = {};
    viewContext.Templates = {};

    viewContext.Templates.Footer = "<div><input type='button' Value='Start Workflow on ID for Selected' onclick='StartWorkflowOnSelectedItems();'></div>";
  
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(viewContext);

})();

function StartWorkflowOnSelectedItems()


  $('.s4-itm-selected').each(function(){   

    var listitemid = $(this).find('.ms-vb-lastCell')[0].innerText; 
    startworkflowonlistitemcsr("Workflow Name", listitemid , "List Name");

  });

}

function startworkflowonlistitemcsr(wfName, itemId, listName) {
  

    var associationData = '';
    var webMethod = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/NintexWorkflow/Workflow.asmx';
    var soap = "<?xml version=""1.0"" encoding=""utf-8""?>" +
                            "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=""http://www.w3.org/2001/XMLSchema" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/">" +
                              "<soap:Body>" +
                                "<StartWorkflowOnListItem xmlns=""http://nintex.com">" +
                                  "<itemId>" + itemId + "</itemId>" +
                                  "<listName>" + listName + "</listName>" +
                                  "<workflowName>" + wfName + "</workflowName>" +
                                  "<associationData></associationData>" +
                                "</StartWorkflowOnListItem>" +
                              "</soap:Body>" +
                            "</soap:Envelope>";

    $.ajax({
        type: "POST",
        url: webMethod,
        beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://nintex.com/StartWorkflowOnListItem"); },
        data: soap,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml",
        success: function (data) {
           
        },
        error: function (e) {         
           
        }

    });
}

 

Jan

Badge +3

Jan,

Thanks so much. In this case, the need for this has receded (it had been voiced by someone other than those who would actually use the feature), but I can absolutely see the need for this capability for several other processes we have.

Regards,

   John

Badge +7

Hi,

This is SharePoint's client side rendering logic, you'll need to upload the script to the master page gallery and then reference it in the JS Link property on the view web part. An overview is here, Introduction to Client-Side Rendering in SharePoint 2013 | Kaptyn

Jan

Reply