Link to home
Create AccountLog in
Avatar of Pieter Nieuwkamer
Pieter NieuwkamerFlag for Netherlands

asked on

Sharepoint javascript only runs when page is in edit mode

I have a page where I can start a site workflow via a html hyperlink. On this page a javascript file is loaded which has the StartWorkflow4 javascript code.
Clicking the link displays the "Waiting for workflow" message but the workflow itself is not started.
When the page is in edit mode and I click the link the workflow is executed correctly.
I read this has something to do with sp.js not being loaded in display mode but I don't know where I can load it.

This is the code in my js file:

var errorMessage = "Something went wrong. To try again, reload the page and then start the workflow.";
      var theForm = document.forms['aspnetForm'];
      if (!theForm)
      {
            theForm = document.aspnetForm;
      }
      function StartWorkflow(iwa)
      {
            var elIwaStart = document.getElementById("iwaStart");
            elIwaStart.value = iwa;
            theForm.submit();
      }
      var dlg = null;
      function StartWorkflow4(subscriptionId, itemId, itemGuid)
      {
            showInProgressDialog();
            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)
                  {
                        var params = new Object();
                        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)
                              {
                                    closeInProgressDialog();
                                    var elWf4Start = document.getElementById("wf4Start");
                                    elWf4Start.value = 1;
                                    theForm.submit();
                              },
                              function (sender, args)
                              {
                                    closeInProgressDialog();
                                    alert(errorMessage);
                              }
                        );
                  },
                  function(sender, args)
                  {
                        closeInProgressDialog();
                        alert(errorMessage);
                  }
            );
      }

      function closeInProgressDialog()
      {
            if(dlg != null)
            {
                  dlg.close();
            }
      }

      function showInProgressDialog()
      {
            if(dlg == null)
            {
                  dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait...", "Waiting for workflow...", null, null);
            }
      }

      function HandleCheckinBeforeStartWorkflow()
      {
            var strError = "Please check this document in before starting a workflow.";
            window.alert(strError);
      }
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

I supose that this will not be the only one js library missing.
Look into page elements in Developer Console by pressing F12 in your browser and try to load first the sp.js and afterwards all missing libraries which generate error messages in the Developer Console.

Or start with this:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', 
        function (){
            console.log("SP.js now loaded.");
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pieter Nieuwkamer
Pieter Nieuwkamer
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer