troubleshooting Question

Sharepoint javascript only runs when page is in edit mode

Avatar of Pieter Nieuwkamer
Pieter NieuwkamerFlag for Netherlands asked on
JavaScriptHTMLMicrosoft SharePoint
2 Comments1 Solution30 ViewsLast Modified:
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);
      }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros