How can i get the task id of a workflow?
-
08-02-2021 - |
質問
How can i get the task Id of a workflow task?
I tried to get it by using paralel blocks, like this
With the parallel block, I do two things:
- Create the task
- Get the task ID with Rest API
To get the Task ID, I use the field "WF4InstanceId", with a descending order, by Id and taking only the first row and before that I wait for 2 minutes.
Request URL:
_api/web/lists/getByTitle('*ListName*')/items?$select=ID,WF4InstanceId&$filter=WF4InstanceId eq '*WorkflowInstanceId'&$top=1&$orderby=Id desc
Using the Request URL with a extension, it throws the correct row
But in the workflow it doesn't.
And, if do the request (With the Request URL of the workflow history), it works
Also working directly in the chrome but in xml. (Because lack of headers)
I don't know what can i do to fix that or if there is another way to get the task id.
Can someone help me?
解決
Here is details descriptive steps on how to get task id from SharePoint designer workflow:
The workflow needs to perform some actions in parallel, sort of, and a “HTTP Web Service Action”. The web service can get the ID of the latest Task by using the following URL
[%Workflow Context:Current Site URL%] /sites/mySite/_api/web/lists/getbytitle('tasks')/items?$top=1&$orderby=Id%20desc
Using this URL in a “Call HTTP web service” to return “ResponseContent”
The next action is get item from dictionary
Get d/results(0) from Variable: ResponseContent (Output to Variable: ResponseContent )
Following with this
Get Id from Variable: ResponseContent (Output to Variable: TaskId )
This can only work if the workflow runs in a parallel block but with a tweak to run slightly after – this ensures the task item is definitely there.
Setting up the call web service can be done in this way
Do the web service setup steps as normal then add a parallel block
Add and configure start task process into it
The following actions will run in parallel: Start a task process with Variable: Region (Task outcome to Variable: Outcome1 )
Then add an If block
If Current Item:ID is greater than 0
This allows a run of actions sequentially (but still in parallel to the task). Without this the next steps would all trigger at the same time.
Add a pause before continuing just to ensure the task IS created
Pause for 0 days, 0 hours, 1 minutes
Then call the web service
Call [URL]... HTTP web service with request (ResponseContent to Variable: ResponseContent |ResponseHeaders to responseHeaders |ResponseStatusCode to Variable: responseCode1 )
Manage the results
Get d/results(0) from Variable: ResponseContent (Output to Variable: ResponseContent ) Get Id from Variable: ResponseContent (Output to Variable: TaskId )
You can then update an item in a list using the newly found TaskId
Reference :
SharePoint Designer 2013 Workflows – How to get Task Process ID