Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAP GUI Scripting with SNC/SSO

Tags:

c#

sap

I'm trying to log in to SAP with SNC connection and with username and password (not automatically) using SAP GUI Scripting. On my connections list there is my destination server with auto logon set to true. I know that it is possible to add the same server again and set auto logon to false and choose it, but client doesn't want to add another position to list. Normally (without SNC) I just use

CSapROTWrapper sapROTWrapper = new CSapROTWrapper();
var SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
var engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
var GuiApp = (GuiApplication)engine;
var connection = GuiApp.OpenConnection("My server name", true, true);

and then I can put username and password. With SNC and autologon however I am already logged in using credentials put in SNC (and I want to use different credentials without doing anything with SNC). I tried using

var connection = GuiApp.OpenConnectionByConnectionString("my.server.address", true, true);

but it fails as it doesn't connect using SNC (which is required). I tried to build connection string in java client way, which I found somewhere on the Internet:

/H/ip.add.res.s/S/3200&sncon=true&sncname=properName&sncqop=4

but each time connection is not established with information:

The 'Sapgui Component' could not be instantiated

I went through multiple SAP documents but found no information about passing SNC parameters.

The final question is: Is there a way to connect to SAP GUI using code with SNC but without autologon?

like image 456
Piotr Wojsa Avatar asked Oct 17 '22 07:10

Piotr Wojsa


1 Answers

The problem was easier to solve than I expected. I guess I spent too much time and I didn't check the easiest option. Using connection string was correct but I couldn't find my connection string. To find it I just needed to start SAP with proper settings and read connection string like below:

var sapROTWrapper = new CSapROTWrapper();
var sapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
var engine = sapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, sapGuilRot, null);
var guiApp = (GuiApplication)engine;
var existingConnection = (GuiConnection)guiApp.Connections.ElementAt(0);
var properConnectionString = existingConnection.ConnectionString;

Then all I needed to do was to save this connection string and rework login to use it.

var sapROTWrapper = new CSapROTWrapper();
var sapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
var engine = sapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, sapGuilRot, null);
var guiApp = (GuiApplication)engine;
var connectionString = "connection string read from previous run";
var connection = guiApp.OpenConnectionByConnectionString(connectionString, true, true);

Now I can use SAP with SNC without SSO.

like image 71
Piotr Wojsa Avatar answered Nov 15 '22 05:11

Piotr Wojsa