Next we describe how to write to files (in the internal filesystem) from a script. Basically there are two ways to accomplish this. We have already seen how to work with persistent objects (or rather entities) such as users. A file is just such an entity:
The corresponding service is the FileServerService. Thus, we could create a new file and store it. There is, however, a simpler way using the GLOBALS object. The GLOBALS object comes with a dedicated fileService object, that allows to easily read from and write to files.
As a first step we construct a new text file. We open the terminal and go to the fileserver and create a new temporary folder (e.g. tmp with mkdir tmp). To write some text into a new file we can simply use the following terminal command
echo foobar > file.txt
The echo command prints its arguments and the angular opening bracket forwards this output into the file file.txt. A single angular bracket will overwrite the contents of the file. To append text to the file use two angular brackets, for example:
echo more foobar >> file.txt
To inspect the created file you can either use the editTextFile command or use the "cat" command which simply dumps the text file to the terminal.
Next, we are going to write a simple script that is going to emulate the cat command. We will call this script myCat.rs. For this create the script using
Remember that scripts need to be located beneath the bin folder. So if you created your script outside of the bin hierarchy move it there using the mv command. To emulate the cat command we only need a single line as the GLOBALS object offers some helper methods to read in files.
The read method takes a location of a file and returns the contents of the file as a string. Similarly the write method of the GLOBALS object allows to write to a text file. Note that this will overwrite the data within the file. Thus the following script emulates the terminal command "echo TEXT > file"
This could then be called, for example as
exec -c myWrite.rs file.txt 'This is some text'
Note the use of the -c flag to commit the changes and the use of single quotes to treat 'This is some text' as a single argument.
The read and write method of the GLOBALS object will always return (or expect) a string. If you want to work with binary files you can use the fileService. Via the globals object you have access to all Services provided by ReportServer. Additionally, there are a couple of services specifically for working with scripts. These are accessed via the services variable of the GLOBALS object. That is, we could have written our myCat.rs script also as
In addition to the read and write methods the fileService has additionally the methods readRaw and writeRaw that allow to work directly with byte representations of the files.
Besides the fileService there are other services specifically available for script developers to help facilitate writing scripts. We will encounter them in the course of this manual. However, here is a short overview of the services available via GLOBALS.services
|fileService||easy access to read and write files.|
|scriptService||provides methods to call scripts from within your script.|
|registry||A singleton registry that can be used to store and retrieve arbitrary values. The registry is held in memory and cleared on ReportServer restarts.|
|clientExtensionService||An interface to access client side extensions.|