Article: AN0002009Updated: 23.09.2018
This functionality enables to create number of scripts that can be called internally or from an external system. Each script belongs to a single model and has a global code (unique in the whole ObjectGears system instance over all the models), by means of which the script is called.
It is possible to disable the script immediatelly be means of property Enable. Scripts can be called internally (by means of function OG.Script.RunScript) or from an external system (by means of web service RunScript or http request for page RunScript.aspx). When an external call is used, the script has to be respective call enabled. Furthermore, the user has to have assigned role defined at the script.
Script can be executed with parameters. These parameters are available in the script by means of property OGParameters.
var value = OGParameters['parameter_name'];
Call by means of HTTP request
Page RunScript.aspx is used for executing a script by means of HTTP request. Add parameter Code, that contains the code of the executed script, into URL.
Whatever further parameters are passed over to the script in hashtable in property OGParameters.
The call result depends on parameters of the object ResponseContent. If an error occurred during script processing or property FileName is not filled in at object ResponseContent, then the answer is following:
Value |
Description |
OK |
Script was executed successfully. |
ERROR: xxx |
An error xxx occured at the script execution. |
Otherwise content of property ContentBytes or ContentText is returned. When encoding is set (property Encoding) also Encoding of the answer is set.
Example of calling a script with code UserTest:
http://www.yourserver.com/RunScript.aspx?code=UserTest
Script examples
This example shows reading parameter from a call and setting up content of a returned file. The example skips parameter validation and reading the file content from the database.
var c = OGParameters['c'];
var v = OGParameters['v'];
if ( c == null || v == null)
{
OGResponseContent.ContentText = 'ERROR:Parameters C and V are not defined.';
return;
}
...
OGResponseContent.FileName = 'myfile.txt';
OGResponseContent.Encoding = OG.Encoding.GetUTF8();
OGResponseContent.ContentText = '...file content...';
This example shows reading file from a record and its return as a response. The script contains firmly defined class with id 35 and column with id 578 due to simplification.
//return ZIP file
var fiList = OG.DataRowFile.GetFileInfo( 35, 578, dr.Id);
if ( fiList != null && fiList.Count > 0)
{
OGResponseContent.ContentBytes = OG.DataRowFile.GetFileContent(fiList[0].Id, 35);
OGResponseContent.FileName = 'package' + fiList[0].Id + '.zip';
}
else
{
OGResponseContent.ContentText = 'ERROR: There is no file at the record.';
OGResponseContent.FileName = 'error.txt';
}