4

I created a script editor webpart. I added a button which start a js function in the onClick event. I would like to start a workflow which is implemented on some list. Is this possible?

  • Do you want to trigger workflow on particular list item? – Dhaval Kariya Jun 14 '17 at 13:52
  • Do you want to add a custom button that user will click when he wants to trigger workflow versus item added or updated? – Venkat Konjeti Jun 14 '17 at 13:52
  • pls mark my answer if it has solved your problems – DvG Jun 21 '17 at 9:29
3

I would take a look at what the URL is from the Item > Workflows > Start Workflow option on the specific one you want to run, that's the only way I can think of to start it but it would be a nightmare to make sure the workflow GUID and item ID are correct in the URL, so I would maybe try to find some other way to do this.

For the sake of a few clicks, it seems like a lot of effort for not very much gain.

3

Absolutely possible, You can use SPServices.Js to achieve this. Actually you need to create a specific URL that will redirect you to WF Initiation page. It will have structure Like:

*../sites/your_sitename/layouts/15/IniWrkflIP.aspx?List="+your_List_guid+"&ID="+<your_ItemID>+"&TemplateID={"+WFguid+"}&Source=https://....";*

Here Last parameter &Source= is optional. On click of a button, I will do manipulation and redirect you to Workflow initiation page

$('.btn').click(function(){
var path;
var ItemID;
var context = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
var list = context.get_web().get_lists().getByTitle("xxxxxxxx");
var item;

//to get list GUID -- start
var ListID = "";
$().SPServices({
operation: "GetList",
listName: "xxxxxxxx",
async: false,
completefunc: function (xData, Status) {
ListID = $(xData.responseXML).find("List").attr("ID");
}
});
//to get list GUID -- end

//to get Template ID of workflow -- start
var itemurl;
var f = "<ViewFields>"+
                '<FieldRef Name="EncodedAbsUrl"/>'+
            "</ViewFields>";
$().SPServices({
operation: "GetListItems",
async: false,
listName: "xxxxxxxx",
CAMLViewFields: f,
ID: ItemID,
completefunc: function (xData, Status) {
  $(xData.responseXML).SPFilterNode("z:row").each(function() {
      itemurl = $(this).attr("ows_EncodedAbsUrl")
    });
  }
});
var guid;
$().SPServices({
  operation: "GetTemplatesForItem",
  item: itemurl,
  async: false,
  completefunc: function (xData, Status) {
    var currentItemURL = this.item;
    $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function (i, e) {
      if ($(this).attr("Name") == "your WF display name") {
        guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");
       }
    });
  }
});
//to get Template ID of workflow -- end


path="<siteUrl>/sites/xxxx/_layouts/15/IniWrkflIP.aspx?List="+ListID+"&ID="+ItemID+"&TemplateID={"+guid+"}&Source=<URL where you want to redirect post WF action>";

window.open(path,'_self');
});
  • This is what I was getting at, I just wasn't sure of how you'd pull down the neccessary values! – Thomas Gass Jun 14 '17 at 14:32
  • I think my code is helpful for you to get all required details? please mark this as answer if it solved your issue. Do let me know in case of query! :) – DvG Jun 14 '17 at 14:35

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.