C#からSAPの処理を実行するためのサンプルプログラム作ってみます。
準備
以下のURLからSAP .Net Connectorをダウンロードします。(Sユーザが必要です)
https://service.sap.com/connectors
開いたページから以下の項目でダウンロード可能です。
SAP Connector for Microsoft .NET > Download SAP Connector for Microsoft .NET Version 3.0
ダウンロードしたZIPファイルを展開すると以下の通り。
NCo3016_Net40_x64.msi NCo30APIDocumentation.chm readme_libraries.txt release_notes.txt
MSIファイルでインストールできるのですが、GACに登録しない場合は以下のフォルダにDLLがインストールされます。このDLLを使って開発を進めます。
C:\Program Files\SAP\SAP_DotNetConnector3_Net40_x64\
libicudecnumber.dll rscp4n.dll sapnco.dll sapnco_utils.dll
SAP側のモジュールを探す
リモート実行可能な汎用モジュールを呼び出す手段を書こうと思いますので、BAPIを探してみます。今回もまたユーザの一覧取得します。
では、トランザクションコード「BAPI」から探してみます。
このBAPI_USER_GETLISTが使えそうです。
中身見てみます。インプットのMAX_ROWSで出力行数を制御できるようなので使ってみます。
アウトプットのUSERLISTにユーザの一覧が出力されそうです。これを使います。
USERNAMEの項目を一覧してみることにします。
開発
Visual Studioで新規のC#コンソールアプリケーションを作成します。
プロジェクトの参照に上記でダウンロードしたSAP .Net Connectorよりsapnco.dllを追加しておいてあげてください。
ざっと書いてみました。
using System; using System.Collections.Generic; using System.Linq; using SAP.Middleware.Connector; namespace GetSAPUserList { class Program { static void Main(string[] args) { //引数処理 var option = new Dictionary<string, string>(); for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-h": option[RfcConfigParameters.AppServerHost] = args[++i]; break; case "-i": option[RfcConfigParameters.Name] = args[++i]; break; case "-n": option[RfcConfigParameters.SystemNumber] = args[++i]; break; case "-c": option[RfcConfigParameters.Client] = args[++i]; break; case "-u": option[RfcConfigParameters.User] = args[++i]; break; case "-p": option[RfcConfigParameters.Password] = args[++i]; break; case "-maxrow": option["-maxrow"] = args[++i]; break; default: break; } } //SAP接続用のパラメータ設定 RfcConfigParameters rfcConfigParams = new RfcConfigParameters(); rfcConfigParams.Add(RfcConfigParameters.Name, option[RfcConfigParameters.Name]); rfcConfigParams.Add(RfcConfigParameters.AppServerHost, option[RfcConfigParameters.AppServerHost]); rfcConfigParams.Add(RfcConfigParameters.SystemNumber, option[RfcConfigParameters.SystemNumber]); rfcConfigParams.Add(RfcConfigParameters.Client, option[RfcConfigParameters.Client]); rfcConfigParams.Add(RfcConfigParameters.User, option[RfcConfigParameters.User]); rfcConfigParams.Add(RfcConfigParameters.Password, option[RfcConfigParameters.Password]); var destination = RfcDestinationManager.GetDestination(rfcConfigParams); //SAP処理の実行 string functionModuleNmae = "BAPI_USER_GETLIST"; var sapFunction = destination.Repository.CreateFunction(functionModuleNmae); if(option.ContainsKey("-maxrow")) { sapFunction.SetValue("MAX_ROWS", option["-maxrow"]); } sapFunction.Invoke(destination); //SAP処理結果出力 var userList = sapFunction.GetTable("USERLIST"); userList.ToList().ForEach(row => { Console.WriteLine(row.GetValue("USERNAME")); }); } } }
実行方法
早速実行してみます。
>GetSAPUserList.exe -h my-erp-hostname -i PRD -n 00 -c 800 -u sapuser -p passw0rd -maxrow 10 SAPUSER01 SAPUSER02 SAPUSER03 SAPUSER04 SAPUSER05 SAPUSER06 SAPUSER07 SAPUSER08 SAPUSER09 SAPUSER10
以上。サンプルプログラムでした。