5

I'm using SharePoint 2010 Foundation. I've developed a SharePoint-Farm Solution and now I want start a SharePoint Workflow clicking a Ribbon Button.

I know that with SharePoint Designer I can create a Ribbon Button that start a SharePoint Workflow but I've defined my Ribbon button inside my solution and I would perform this operation declaratively.

How can I perform this operation?

Thanks for your support.

5

you can use $().SPServices

<script type="text/javascript" src="/sites/sprc/Resources%20%20jQuery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/sites/sprc/Resources%20%20jQuery/jQuery%20SP%20Services/jquery.SPServices-0.5.4.min.js"></script>
<script type="text/javascript">

function StartWorkflow(ItemURL, ItemID) {
  var loadingImage = ‘Loader’ + ItemID;
  var workflowDiv = ‘WorkflowDiv’ + ItemID;
  //Show our loading image
  document.getElementById(loadingImage).style.visibility = ‘visible’;
  $().SPServices({
    operation: "StartWorkflow",
    item: ItemURL,
    templateId: "{04ee1c93-f6b7-49b3-a79c-fa3142ecd688}",
    workflowParameters: "<root />",
    completefunc: function() {
      document.getElementById(workflowDiv).innerHTML = ‘Workflow Started’;
    }
  });
}
</script>

SPServices
Start a workflow with Sharepoint (JavaScript)

You should also take a look to Workflow WebServices (/_vti_bin/workflow.asmx).
A solution can be create a Web Method (within an application page) that calls the method StartWorkflow. (you can call the web method from $.Ajax).

Workflow Web Service
Workflow Members

  • Hi, thanks for your answer. Have you tried this code with SharePoint 2010? I know that I can execute this code only with SharePoint 2013. – ilMattion Apr 20 '15 at 16:57
  • Update the answer with an other possible solution – Nk SP Apr 20 '15 at 18:26
  • I was able to use Workflow.asmx and StartWorkflow method in SP2010 to run Nintex workflow, so SP OOB workflow should run as well. – Maksymilian Mulawa Apr 25 '15 at 19:07
2

You can use this function:

    function scriptStart(xSelectedItemId, xSubscriptionId) {

    //var xSubscriptionId = "{7d028513-b2ea-4ad0-a996-9b807f2beb2e}"
    console.log("scriptStartWF.js: Ribbonbutton script started, Workflow subscriptionId==" + xSubscriptionId + ", selected Item Id==" + xSelectedItemId)
    var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.js", function () {
            $.getScript(scriptbase + "SP.WorkflowServices.js", function () {
                StartWorkflow(xSubscriptionId, xSelectedItemId);
            });
    });
}

//dialog element to show during processing
//var dlg = null;

//Subscription id - Workflow subscription id --> looked it up in App, does not change.
function StartWorkflow(subscriptionId, itemId) {
    fxShowInProgressDialog("Starten Workflow...");
    var ctx = SP.ClientContext.get_current();
    var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
    var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);
    ctx.load(subscription, 'PropertyDefinitions');
    ctx.executeQueryAsync(
        function (sender, args) {
            console.log("scriptStartWF.js: obtaining Workflow Property Definitions...");
            var params = new Object();

            //Find initiation data to be passed to workflow.
            var formData = subscription.get_propertyDefinitions()["FormData"];
            if (formData != null && formData != 'undefined' && formData != "") {
                var assocParams = formData.split(";#");
                for (var i = 0; i < assocParams.length; i++) {
                    params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]];
                }
            }
            if (itemId) {
                wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);
            }
            else {
                wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);
            }
            ctx.executeQueryAsync(
                function (sender, args) {
                    console.log("scriptStartWF.js: Workflow Started...")
                    fxCloseInProgressDialog();
                },
                function (sender, args) {
                    fxCloseInProgressDialog();
                    alert('Failed to run workflow, Error: ' + args.get_message());
                }
            );
        },
        function (sender, args) {
            fxCloseInProgressDialog();
            alert('Failed to run workflow, Error: ' + args.get_message());
        }
    );
}

function fxShowInProgressDialog(msg) {
        dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Een ogenblik a.u.b...", msg, null, null);
}

function fxCloseInProgressDialog() {
        dlg.close();
}

The XML of my Ribbon button looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <CustomAction Id="ScriptLink.jQuery" Location ="ScriptLink" ScriptSrc="~site/Scripts/jquery-2.1.1.js" />
  <CustomAction Id="Ribbon.Library.Actions.Scripts" Location ="ScriptLink" ScriptSrc="~site/Scripts/scriptStartWF.js" />

  <CustomAction Id="d1d06df8-d6aa-40ff-8e5c-eeaed3ccbb0e.RibbonBtnStartWfNewWI"
                RegistrationType="List"
                RegistrationId="101"
                Location="CommandUI.Ribbon"
                Sequence="10001"
                Title="Invoke &apos;RibbonBtnStartWfNewWI&apos; action">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button Id="Ribbon.Documents.Manage.RibbonBtnStartWfNewWIButton"
                  Alt="Request RibbonBtnStartWfNewWI"
                  Sequence="100"
                  Command="Invoke_RibbonBtnStartWfNewWIButtonRequest"
                  LabelText="Start Workflow Werkinstr."
                  TemplateAlias="o1"
                  Image32by32="_layouts/15/images/placeholder32x32.png"
                  Image16by16="_layouts/15/images/placeholder16x16.png" />
        </CommandUIDefinition>
      </CommandUIDefinitions>

      <CommandUIHandlers>
        <CommandUIHandler Command="Invoke_RibbonBtnStartWfNewWIButtonRequest" 
                          CommandAction="javascript: 
                                        var ctx = SP.ClientContext.get_current();
                                        var selectedItems = SP.ListOperation.Selection.getSelectedItems(this.ctx);
                                        var cnt = selectedItems.length; 
                                        if (cnt == 1) {
                                          scriptStart({SelectedItemId}, '{7d028513-b2ea-4ad0-a996-9b807f2beb2e}'); 
                                        }
                                        else {
                                          alert(cnt + ' items geselecteerd. Selecteer precies 1 item a.u.b.');
                                        }"
                          />    
      </CommandUIHandlers>
    </CommandUIExtension >
  </CustomAction>
</Elements>

for more info check blogpost

  • Thanks for solution, but I don't think it will work with 2010 workflows. It works with 2013 workflows only. – Roman Apr 19 '15 at 18:45
  • mmm didn't know that, good luck anyway! – tomvanderhoek Apr 20 '15 at 14:11
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script> 
<script src="../../SiteAssets/jquery.SPServices-2014.02.min.js" type="text/javascript"></script> 
<script src="../../SiteAssets/jquery.SPServices-2014.02.js" type="text/javascript"></script>
<script type="text/javascript">
 $("#btnSubmit").click(function() { $().SPServices({ operation: "StartWorkflow", item:"../../Workflows/SubmitWF/SubmitWF.xoml", templateId:"{04ee1c93-f6b7-49b3-a79c-fa3142ecd688}", worflowParameters:"<root/>" }); }); </script> 

http://ctp009602c:9090/sites/fcsm/Draft%20Document/Forms/CustomDisplay.aspx Doesn't call workflow in sharepoint 2013....

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.