Skip to main content
12-Amethyst
July 14, 2017
Solved

Set session parameter from javascript

  • July 14, 2017
  • 2 replies
  • 9309 views

Does anyone know how to set a Session Parameter using javascript?  I would like to create a custom widget that sets a session parameter.  I assume I have to access the User Management Subsystem but I'm not sure how to do that in javascript.

Thank you

Best answer by sclement1

Thank you for the response.

The problem I have is that at runtime the javascript cannot see the Resources[] object.  So any call to it returns an error.  What you can do however is:

TW.Runtime.Session.SetSessionValue(<session parameter>,<value>);

However for my purposes I think using a Mashup Parameter is easier to implement.  To set the value of a mashup parameter on the current mashup:

TW.Runtime.Workspace.Mashups.Current.setParameter(<mashup parameter name>,<);

The custom widget I created is based off the shape widget and sets the Mashup Parameter value specified in it's properties to the value specified in it's properties.

2 replies

13-Aquamarine
July 14, 2017
sclement112-AmethystAuthor
12-Amethyst
July 14, 2017

Janet,

  Thank you for the suggestion.  It got me thinking down the right path.  The document describes how to create a Session Parameter but I needed to give that Session Parameter a value at runtime without binding to it.  Instead of using a Session Parameter I ended up using a Mashup Parameter which probably works out better.

  I created a "smartShape" widget that allows you to send value to a Mashup Parameter when you mouse over the widget.  I used the TW.RunTime.WorkSPace.Mashups.Current.setParameter function to set the Mashup Parameter to a value specified in the smartShape widget's properties.  The purpose is to allow me to create a Mashup that can react appropriately when an area of the screen is clicked without having to bring in the same instance of a service multiple times.  For my current purposes the smartShapes are mostly transparent but when you click on one the color changes to red to indicate that section of the screen has been clicked.  They all call the same instance of a service when clicked.  The service uses the value in the Mashup Parameter to determine which shape was clicked.

16-Pearl
July 14, 2017

Try this

   var myValue = inComingValue ;

    try {

        var params = {

            name: "mySessionBoolean" /* STRING and must exist as a Session defined property */,

            value: myValue  /* BOOLEAN */

        };

        Resources["CurrentSessionInfo"].SetGlobalSessionBooleanValue(params); // Use SetGlobalSessionXXXX based on your type

        logger.debug(me.name+" Set Session: set mySessionBoolean to " + myValue );

       

    } catch(err) {

        logger.error(me.name+" Set Session mySessionBoolean : Unable to set session mySessionBoolean Error: " + err);

    }

sclement112-AmethystAuthorAnswer
12-Amethyst
July 17, 2017

Thank you for the response.

The problem I have is that at runtime the javascript cannot see the Resources[] object.  So any call to it returns an error.  What you can do however is:

TW.Runtime.Session.SetSessionValue(<session parameter>,<value>);

However for my purposes I think using a Mashup Parameter is easier to implement.  To set the value of a mashup parameter on the current mashup:

TW.Runtime.Workspace.Mashups.Current.setParameter(<mashup parameter name>,<);

The custom widget I created is based off the shape widget and sets the Mashup Parameter value specified in it's properties to the value specified in it's properties.

1-Visitor
December 27, 2017

Is that supposed to be TW.Runtime.Workspace.Mashups.Current.setParameter(<mashup parameter name>,<value>); ?

Are there any links to documentation for this and other calls in Thingworx, I can't find any good documentation.

When I try to use this, it doesn't seem to work.  It should be useable in an expression widget correct?

Thanks.