Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopWorkflow does not work inside NodeJS : ReferenceError: document is not defined #162

Closed
saunlani opened this issue Jan 28, 2021 · 16 comments

Comments

@saunlani
Copy link

saunlani commented Jan 28, 2021

This is the error I receive when attempting to use stopWorkflow in an external NodeJS API:

my execution:
sp.list("ListName","http://company.com/").stopWorkflow({ID:6736, workflowName:"2010 Workflow"});

the error:

(node:14288) UnhandledPromiseRejectionWarning: ReferenceError: document is not defined
    at C:\Repos\node_modules\sharepointplus\dist\lists\stopWorkflow.js:50:7
    at new Promise (<anonymous>)
    at new Wrapper (C:\Repos\node_modules\core-js-pure\internals\export.js:15:24)
    at C:\Repos\node_modules\sharepointplus\dist\lists\stopWorkflow.js:48:12
(node:14288) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was
not handled with .catch(). (rejection id: 2)

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 28, 2021

There is no API to stop a workflow. So as a workaround the script opens an iframe to the Workflow Instance page where there is the "stop workflow" option. And then the form is posted to cancel the workflow.

In a Node environment, I'm not sure right now. I'll have to do some research to see how it's possible to have the same kind of behavior.

@saunlani
Copy link
Author

I see, yeah this function is actually my main purpose behind using your package. So any help at all is greatly appreciated!

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 28, 2021

I'll need a few days to find the time to work on it.

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 29, 2021

I don't have the bandwidth right now to investigate. If you cannot wait for a couple of weeks, then I'll invite you to explore by yourself the below solutions:

You have to use $SP().getWorkflowID() to get the instances: you need the last instance that contains an url. Something like:

async function run() {
  let params = await $SP().getWorkflowID({ID:itemID, workflowName:"The workflow name"});
  if (params.instances.length===0) return "No instance available for this workflow/item ID.";
  let pageUrl = params.instances[params.instances.length-1].StatusPageUrl;

  // then call "pageUrl" with either `jsdom` or a puppeter script
  // once the page is loaded, you need to trigger the below from the page:
  // window.__doPostBack('ctl00$PlaceHolderMain$HtmlAnchorEnd','')
}

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 29, 2021

I took some time to find out a solution. It took me one hour, and here is how to proceed: https://github.com/Aymkdn/SharepointPlus/wiki/Stop-a-workflow-2010-from-Node-Server-Application

@Aymkdn Aymkdn closed this as completed Jan 29, 2021
@saunlani
Copy link
Author

seriously, thank you very much for resolving this so quickly!

@saunlani
Copy link
Author

saunlani commented Feb 1, 2021

I'm noticing this stopWorkflow solution works perfectly for workflows that were automatically started. For workflows that were manually started, the stopWorkflow function will not successfully stop the workflow.

Can you please confirm that this is working for you with workflows that have not been automatically started on item creation?

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

I don't know. I'll have to create a list and a workflow and test.

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

I've just tried, and it worked as expected: I manually started a workflow (that is doing a "pause for 5 hours") then I used the code to stop it, and it worked.

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

It's using the "End this workflow" link from a Workflow Status page. So if you can see this link for the workflow/item you want to stop, then it's supposed to work:
image

@saunlani
Copy link
Author

saunlani commented Feb 1, 2021

Interesting. Can you please try starting two workflows and using a for loop to iterate through them and stop both of them?
I'm not seeing an error when it tries to stop the workflows, but I'm also noticing it's not actually stopping the second workflow.

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

Sorry, I don't have the time to do it. Please, refer to my previous comment.

@saunlani
Copy link
Author

saunlani commented Feb 1, 2021

Yeah I've tested this thoroughly and it seems like the stopWorkflow function is only working for the very first workflow. It has no effect on other workflows.

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

It seems there is an issue in my code as the getWorkflowID will only return the data for the first workflow. I'll have to fix it.

@Aymkdn
Copy link
Owner

Aymkdn commented Feb 1, 2021

I've just published v6.1.4 that fixes $SP().list().getWorkflowID() and also that makes $SP().list().stopWorkflow() works in Node app (so no need to use the wiki page that I'm going to delete)

@saunlani
Copy link
Author

saunlani commented Feb 2, 2021

Thanks again sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants