How to get the UserID/Database/SqlServer in VS Tools.
We need to wait a little bit to go looking for these items, GpAddIn.vb might Initialize before the user has logged in and selected a database. Too, what if the user changes databases while logged in?
First, here is the App class that I bring into every project:
Public Class App
Public Shared AppName As String
Public Shared StartupPath As String = CurDir()
Public Shared SQLServer As String
Public Shared Database As String
Public Shared UserID As String
End Class
Now, here is a sample GpAddIn.vb class. Note that we set an event handler for the Toolbar.OpenAfterOriginal event. When the toolbar opens, we get the UserID/SQLServer/Database and write them to the App class. They're easily referenced by the application then.
In production code I put the toolbar handler in a separate class; but it works for the demo a little easier this way.
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports Microsoft.Dexterity.Bridge
Imports Microsoft.Dexterity.Applications
Imports FPCommon
Public Class GPAddIn
Implements IDexterityAddIn
' IDexterityAddIn interface
Sub Initialize() Implements IDexterityAddin.Initialize
'register an event handler for the Toolbar.OpenAfterOriginal event
AddHandler Dynamics.Forms.Toolbar.OpenAfterOriginal, AddressOf ToolbarOpenAfterOriginal
End Sub
Sub ToolbarOpenAfterOriginal(ByVal sender As Object, ByVal e As System.EventArgs)
App.AppName = "MyAppName"
App.Database = Dynamics.Globals.IntercompanyId.Value
App.UserID = Dynamics.Globals.UserId.Value
'get the server
Dim backup As Microsoft.Dexterity.Applications.DynamicsDictionary.SyBackupRestoreForm
backup = Microsoft.Dexterity.Applications.Dynamics.Forms.SyBackupRestore
App.SQLServer = backup.Functions.GetServerNameWithInstance.Invoke
End Sub
End Class