This project is read-only.

C# Scripting Module in Workflows

Topics: Customizing Orchard, General
Nov 5, 2013 at 1:25 PM
I am trying to set a cookie in a users browser after they submit a form via workflows. I enabled the C# scripting module and added a decision activity on the "Done" event of a form submission. Here is my activity code :

if(WorkContext.HttpContext != null) { WorkContext.HttpContext.Response.Cookies.Add(new System.Web.HttpCookie("Registered", "True")); SetOutcome("True"); } else { SetOutcome("False"); }

Will this code execute under the same context of the form submission or is it being passed to some other thread? The cookie is not appearing in my browser after the form submission.
Nov 6, 2013 at 8:57 PM
So a little more information on this one. The above code has was not being saved/restored due to the brackets. When the code was being injected into the compiler only a piece of the script was there. For simplicity sake I changed it to this :

WorkContext.HttpContext.Response.Cookies.Add(System.Web.HttpCookie("Registered", "True"));

This was failing. So I removed the System.Web.HttpCookie object and just tried calling the WorkContext and doing nothing with it. This resulted in a successful action. So I am sure the issue is related to the System.Web namespace not being included in this version of Mono or being included with a "using" statement in the module. I tried to add it and also set the assembly to "Copy Local=true" but this did not work either. My issue is I don't have much experience with Mono. Finally, I hacked it by doing this :


And then passing this variable into the Dictionary of objects in the Execute method of DecisionActivity.cs :

_csharpService.SetParameter("registrationCookie", new HttpCookie("Registered", "True"));

This worked and the cookie was added to my browser. Anyone who can offer some help to clean this up, that would be great.
Mar 2, 2014 at 5:46 AM
I believe this occurs because the scripts are parsed through the tokenizer before being evaluated. By default, the tokeniser treats anything within {} as tokens.

In later versions of Orchard (not sure exactly since when) the token format has been updated to be #{} (brackets preceded with a hash) presumably to avoid conflicts with script brackets, however support for the old token format is still provided for backward compatibility.

When the tokeniser evaluates input it firstly looks for any occurrence of "#{" within the input, and if it finds this, it expects that the new token format is to be in use. If it doesn't then it expects the old token format (without the # prefix is in use).

So until support for the old token format is completely removed, if you are writing a script that needs to use brackets, you should make sure there is at least one occurrence of the "#{" string in your script. If you are already referring to tokens within your script, then just make sure you use the new token format and it should work fine. If you don't need to access any tokens within your script, then you can add a dummy token reference in a comment. For example...
// #{}
string someVar = string.Empty;
if ("this will now work fine" == someVar) {
    // Do Something
else {
   // Do Something Else
Mar 5, 2014 at 6:53 PM
Thank you so much @mjy78 for your very useful comment.