Here is a solution in Lawson Smart Office to write “compiled scripts” in C# (as opposed to writing dynamic scripts in JScript.NET).
Background
Traditionally, Personalized Scripts for Lawson Smart Office are written with the built-in Script Editor in the JScript.NET programming language and are deployed as plain text *.js files on the server.
There is also a less known technique. It is also possible to write scripts in C#, to compile them as DLL, and to deploy the *.dll file in lieu of the *.js file.
Pros and cons
There are several advantages of using C# versus JScript.NET. Mostly, the biggest advantage is the full richness of C#. Indeed, C# supports features that are not supported in JScript.NET, for example delegates. Also, C# is more extensively supported by Microsoft and by the community. Whereas JScript.NET is not fully supported by Microsoft, for example there’s no IntelliSense for JScript.NET in Visual Studio whereas there is IntelliSense for C#, and there are almost no examples for JScript.NET in MSDN whereas there are plenty for C#.
There are several disadvantages of using C# versus JScript.NET. Developing compiled scripts requires compiling the C# source code and deploying the DLL to run the script, which makes each iteration longer than developing with JScript.NET. Also, from the deployed DLL it’s not possible to directly see the source code, which is a problem if the source code is not available. Also, the script might need to be re-compiled for different versions of Smart Office, which could be a problem with upgrades.
Microsoft Visual C# Express
I will use Microsoft Visual C# Express to develop and compile my source code.
1) Find the Smart Office libraries
First, we need to find the Smart Office DLL as we’ll need to reference them in Microsoft Visual C# Express.
A user’s computer can run multiple instances of Smart Office at the same time, with different LSO version numbers, and different DLL version numbers. We want to find the correct DLL for our desired instance of Smart Office. There are two sets of DLL to find:
- Go to Smart Office
- Open the Help menu, it’s the question mark icon at the top right
- Select About Lawson Smart Office:
- Select View log file:
- That will open the Log Viewer
- Filter by message C:\ . That will show you the path to the Smart Office ClickOnce deployment folder in your computer, for example:
- C:\Users\12229\AppData\Local\Apps\2.0\Data\79O176HR.9F1\N42AOMBD.0HB\http..tion_2b27e2947dd74766_000a.0000_20a2b87dbe5264e8\
- Open that folder in Windows Explorer
- Search for the DLL files in all sub-folders. That will give us the first set of DLL, for example:
- The second set of DLL files is located in the other branch of the folder structure at C:\Users\12229\AppData\Local\Apps\2.0\. For example:
C:\Users\12229\AppData\Local\Apps\2.0\3YDTAV5W.D22\Q01ZYJYU.RV0\http..tion_2b27e2947dd74766_000a.0000_20a2b87dbe5264e8\
2) Create a Project
Now that we found the two sets of DLL files we can create a C# project in Visual C# Express and reference the DLL.
- Go to Visual C# Express
- Select File > New Project
- Select Class Library and give it a Name:
- Select Project > Properties, and make sure to use the same Target framework as your target Smart Office; for example, I use .NET Framework 4.0:
- Select Project > Add Reference:
- Browse to the first set of DLL found above:
- Then add the second set of DLL:
- Add the .NET components PresentationCore and PresentationFramework:
- Add System.Xaml:
- Add WindowBase:
- Select File > Save All, and choose a location to save your project.
3) Type the source code
using System; using System.Windows; using System.Windows.Controls; using MForms; using Mango.UI; namespace MForms.JScript { public class Thibaud { public void Init(object element, object args, object controller, object debug) { } } }
The result will look like this:
4) Compile
Select Debug > Build Solution:
Visual C# Express will compile the code and produce a DLL file in the \obj\Release\ sub-folder of your project:
5) Deploy
Locate the jscript folder.
For Grid versions of Smart Office, the jscript folder is located in one of the LifeCycle Manager sub-folders, for example:
\\hostname\d$\Lawson\LifeCycleManager\Service\<service>\grid\<profile>\applications\LSO_M3_Adapter\webapps\mne\jscript\
For non-Grid versions of Smart Office, the jscript folder is located in one of the WebSphere sub-folders, for example:
\\hostname\d$\IBM\WebSphere7\AppServer\profiles\MWPProfile\installedApps\MWPProfileCell\MWP_EAR.ear\MNE.war\jscript\
And copy/paste your DLL file there:
6) Execute
Now that the script is deployed on the server, we can execute it:
- Attach the script to the desired panel in Smart Office via Tools > Personalize > Scripts
- Set the Script value to the filename and .dll extension, for example Thibaud.dll:
- Click Add
- Click Save
- Press F5 to load and execute the script
7) Additional source code
If you need the controller, you will need to cast it from object to MForms.InstanceController.
InstanceController controller_ = controller as InstanceController;
If you need the content, you will need to cast it from object to System.Windows.Controls.Grid.
Grid content = controller_.RenderEngine.Content;
Now you can start developing your scripts, for example:
TextBox WRCUNM = ScriptUtil.FindChild(content, "WRCUNM") as TextBox; controller_.RenderEngine.ShowMessage("The value is " + WRCUNM.Text);
Here is a screenshot of my sample script:
UPDATE 2012-04-25: Wayne L. found that we can also use a strongly typed signature for the Init method, so we don’t have to cast the objects anymore:
public void Init(object element, String args, MForms.InstanceController controller, MForms.ScriptDebugConsole debug) { }
Voilà!
Thanks to Peter A.J. for the help.
please is there any way to put database connection in separate file so that all DLL files can use it instead defining it in each DLL.
I mean something like
using myCustomDbClass;
?
LikeLike
Hi, yes, to write separate class you’ll need script assemblies in C#, check https://thibaudatwork.wordpress.com/2012/04/24/compiled-scripts-for-smart-office/ and http://smartofficeblog.com/2013/06/20/script-assemblies-in-mforms/ and https://potatoit.wordpress.com/2011/05/11/calling-your-own-assembly-from-jscript/ . Otherwise, you can load your own assembly with System.Reflection https://thibaudatwork.wordpress.com/2011/08/25/send-sms-from-smart-office/
LikeLike
on last version of SMO MUA web.xml should be changed otherwise You aren’t authorized to use dll’s.
—> *.dll
GET
LikeLike
Thank you! Good to know. As a side note, all scripts should be delivered securely over HTTPS to avoid script injection (currently plain text HTTP). Maybe I should add that as a request for feature.
LikeLike
Hi,
Thanks for share this helpful information.
I am new in SDK and JavaScript, can any one ever try to call SDK from java script.
please let me know as i am not quit aware about it.
Thanks
LikeLike
If by JavaScript you mean JScript.NET, yes, from there we can call assembles written in C#, that’s what we do with every script. The reverse is true too, you can compile a JScript.NET to an assembly and call it from C#. It’s all .NET and interchangeable by the CLR.
LikeLike
Thanks for reply,
can you please share some sample code if possible.
Regards
Farhat Khan
LikeLike
I don’t have sample code right now. There is the SDK documentation, Karin’s blog on the SDK, PotatoIT’s series on the SDK. Basically, follow the SDK documentation to drop the DLLs on the server, then call the assemblies in your script as usual.
LikeLike
Farhat, refer to Smart Office SDK > Developers Guide > Chapter 7 Package and deploy
features
LikeLike