Pieter Nieuwkamer
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("i waStart");
elIwaStart.value = iwa;
theForm.submit();
}
var dlg = null;
function StartWorkflow4(subscriptio nId, itemId, itemGuid)
{
showInProgressDialog();
var ctx = SP.ClientContext.get_curre nt();
var wfManager = SP.WorkflowServices.Workfl owServices Manager.ne wObject(ct x, ctx.get_web());
var subscription = wfManager.getWorkflowSubsc riptionSer vice().get Subscripti on(subscri ptionId);
ctx.load(subscription, 'PropertyDefinitions');
ctx.executeQueryAsync(
function(sender, args)
{
var params = new Object();
var formData = subscription.get_propertyD efinitions ()["FormDa ta"];
if(formData != null && formData != 'undefined' && formData != "")
{
var assocParams = formData.split(";#");
for(var i = 0; i < assocParams.length; i++)
{
params[assocParams[i]] = subscription.get_propertyD efinitions ()[assocPa rams[i]];
}
}
if(itemId)
{
wfManager.getWorkflowInsta nceService ().startWo rkflowOnLi stItem(sub scription, itemId, params);
}
else
{
wfManager.getWorkflowInsta nceService ().startWo rkflow(sub scription, params);
}
ctx.executeQueryAsync(
function(sender, args)
{
closeInProgressDialog();
var elWf4Start = document.getElementById("w f4Start");
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.showWait ScreenWith NoClose("P lease wait...", "Waiting for workflow...", null, null);
}
}
function HandleCheckinBeforeStartWo rkflow()
{
var strError = "Please check this document in before starting a workflow.";
window.alert(strError);
}
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("i
elIwaStart.value = iwa;
theForm.submit();
}
var dlg = null;
function StartWorkflow4(subscriptio
{
showInProgressDialog();
var ctx = SP.ClientContext.get_curre
var wfManager = SP.WorkflowServices.Workfl
var subscription = wfManager.getWorkflowSubsc
ctx.load(subscription, 'PropertyDefinitions');
ctx.executeQueryAsync(
function(sender, args)
{
var params = new Object();
var formData = subscription.get_propertyD
if(formData != null && formData != 'undefined' && formData != "")
{
var assocParams = formData.split(";#");
for(var i = 0; i < assocParams.length; i++)
{
params[assocParams[i]] = subscription.get_propertyD
}
}
if(itemId)
{
wfManager.getWorkflowInsta
}
else
{
wfManager.getWorkflowInsta
}
ctx.executeQueryAsync(
function(sender, args)
{
closeInProgressDialog();
var elWf4Start = document.getElementById("w
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.showWait
}
}
function HandleCheckinBeforeStartWo
{
var strError = "Please check this document in before starting a workflow.";
window.alert(strError);
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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:
Open in new window