StartInstance Methode with DataFields Parameters in REST Request


Badge +1

Hi, how can I call the methode and pass the value for the datafields in my Workflow. Here is the code:

$.ajax ({

              url: 'http://K2Server/k2services/REST.svc/Process/Definitions(K2.WF.RoleApproval_B_RoleApproval)/StartInstance?folio=MyTest',

              method: 'GET',

              contentType: "application/json; charset=utf-8",

              dataType: "JSON",

              cache: false,

              async: true,

              beforeSend: function (XMLHttpRequest) {

                                    XMLHttpRequest.setRequestHeader("Accept", "application/json");

                                   $.support.cors = true; },

              crossDomain: false,

              error: function (data, errors, status) {alert("Error " + status); },

              success: function (data) {alert("Success " + status) } });

 

How can I set the datafields in the code. I have a datafield "owner". What I need to do is to pass the value of this datafield in the request.

Thanks Shadi


12 replies

Userlevel 5
Badge +18

It looks like it is passed as part of the Request Body in a specific XML structure (but only with a POST) as per documentation below:


 


http://help.k2.com/onlinehelp/k2blackpearl/devref/current/default.htm#processinstancesstartinstances.html

Badge +1

Thnaks tin for your reply.

 

Could you please tell me where I can find a code example for that. So far I understod I have to use POST Methode and not the GET one.

 

Do you have an example for that?

 

Thanks a lot.

Badge +1

I know now how to do it:

$.ajax

({

url: 'http://K2Server/k2services/REST.svc/Process/Instances/StartInstance?piDataField=true&piXmlField=true',

type: 'POST',

data&colon; '<?xml version="1.0" encoding="utf-8"?> <w:ProcessInstance xmlns:w="http://schemas.k2.com/worklist/d1" xmlns:p="http://schemas.k2.com/process/d1" ExpectedDuration="20" FullName="K2.WF.RoleApprovalRoleApproval" Folio="' + folio + '" Priority="3"> <p:DataField Name="owner">Owner</p:DataField> </w:ProcessInstance>',

contentType: "application/xml",

dataType: "XML",

beforeSend: function (XMLHttpRequest)

{

$.support.cors = true;

},

error: function (data, errors, status) {alert("Error " + status); },

success: function (data) {alert("Success " + status) }

});

 

Badge +2

Hi SHD and tin,

 

I am trying similar thing, which you ve suggested in my code also, but I cannot see a response back as success.error, none of them is being called in the ajax call and workflow is not started either. I am stuck on this since couple of days, can someone suggest or see what the issue is. Code used:

 

$.ajax

(

{

url: 'http://k2server/K2Services/REST.SVC/Process/Instances/StartInstance?piDataField=true&piXmlField=tru​e',

method: 'POST',

data: '<?xml version="1.0" encoding="utf-8"?><w:ProcessInstance xmlns:w="http://schemas.k2.com/worklist/d1" xmlns:p="http://schemas.k2.com/process/d1" FullName="POCRestCall.WF1WF1" Folio="' + folio + '><p:DataField Name="ID">1</p:DataField></w:ProcessInstance>',

contentType: "application/xml",

dataType: "xml",

beforeSend: function (XMLHttpRequest) {$.support.cors = true;},

error: function (result, error, status) { $("p").html("Error: Workflow Not Started") },

success: function(result){$("p").html("Success: Workflow Started Via Rest Call & data field set")}

});

 

Thanks

 

Badge +1

Hi Punit,

 

try using type instead of method in your call block. For me it didn't work with method but with type. Check my code in my last comment.

 

Regards

Shadi

Userlevel 5
Badge +18

Not sure if it may be contributing to the behavior but it looks like there may be a difference in the XML passed in:


 


Folio="' + folio + '" Priority="3">


 


Folio="' + folio + '>


 


Looks to be missing a double quotes.  This is also assuming that the Priority Attribute is not needed.

Badge +2

Thanks SHD & tin.

 

Even after incorporating your suggestionsm I am still not able to start the workflow.

As I am new to this, just wanted to know, when we try to hit the url this way, do we need to check whether the service is enabled or something of that sort.

Just wante dto undertsand the pre requisite here.

 

Any help is greatly appreciated.

 

 

Userlevel 5
Badge +18

If the code seems to be correct, then one more thing that can result in this behavior is process Start rights.  If you check the Windows Application Events log on the K2 Server, do you see any details when replicating this issue?  If there is an error, it may indicate which account it is trying to Start the workflow with.


 


Where are you calling this code from?  Is this from a published site, or are you just running an HTML page from the browser?  If a published site, where is this hosted (on the same server as K2 or different web server)?  As the user's context may play a role (your account vs perhaps application pool account).   If a published site, there may be authentication settings that my come into play.

Badge

Hi,

I am having a similar issue, I can get my workflow to start in Fiddler with 'Automatic Authentication' enabled , but with my Ajax call to the rest service doesn't work at all. Any ideas would be great?

 

Fiddler Details

http://dlx/K2Services/REST.SVC/Process/Instances/StartInstance?piDataField=true

Header 

Content-type: application/xml
dataType: xml

Body

<w:ProcessInstance xmlns:w="http://schemas.k2.com/worklist/d1" xmlns:p="http://schemas.k2.com/process/d1" FullName="simonsimon" Folio="BOB">
<p:DataField Name="test">1</p:DataField>
</w:ProcessInstance>

 

Ajax Call Example

 

$.ajax
(
{
url: 'http://dlx/K2Services/REST.SVC/Process/Instances/StartInstance?piDataField=true',
type: 'POST',
data: '<?xml version="1.0" encoding="utf-8"?><w:ProcessInstance xmlns:w="http://schemas.k2.com/worklist/d1" xmlns:p="http://schemas.k2.com/process/d1" FullName="simonsimon" Folio="1234"><p:DataField Name="test">1</p:DataField></w:ProcessInstance>',
Contenttype: "application/xml",
dataType: "xml",
beforeSend: function (XMLHttpRequest) { $.support.cors = true; },
error: function (result, error, status) { $("p").html("Error: Workflow Not Started") },
success: function(result){$("p").html("Success: Workflow Started Via Rest Call & data field set")}

});

Badge +2

Hi All,

 

I am facing the same issue as Simon, can someone please help out.

Its working for me with Fiddler but not thru the code.

 

Is there a config setting in authentications ection of K2 web.config, which needs to modified to get this working.

Any help would be greatly appreciated.

Badge +1

Hi,

 

DId you find answer to your problem.

 

Im facing same issue.

 

 

rgds

Userlevel 5
Badge +18

Are you able to provide the code/files being used?  


Where is this being called from?


I was able to test the code in the (StartWorkflow.zip)  successfully from the K2 server.


Also check the Windows Application log to see if account is perhaps missing Start Rights or not.


 


 


 

Reply