Skip to content

Smarteam API programming

May 28, 2014

In order to put the required DLLs into GAC, install Smarteam Editor client program in your dev machine (and web server later for deployment). In this example, I used version V5-6R2012 Service Pack 5. In Visual Studio 2012, create a new console project and add references.

SmarteamAPI References

    Sub Main()
        'Test input (DocumentID & ClassID) & etc.
        Dim ClassID As Integer = 949
        Dim ObjectID As Integer = 232216

        Dim myConfigName As String = "SmTeam32"
        Dim mySmarteamDbPwd As String = "DbPasswordEncrypted"
        Dim tempFolder As String = "C:\Temp"
        Dim StUsername As String = "myUserName"
        Dim StPassword As String = "MyPassword"
        Dim SmarteamEngine As SmApplic.SmEngine = Nothing
        Dim mySession As SmApplic.SmSession = Nothing

        Try
            'Create & init a Smarteam engine
            SmarteamEngine = New SmApplic.SmEngine
            SmarteamEngine.Init(myConfigName)
            SmarteamEngine.ServerMode = True 'Only when you use Smarteam API in a server environment

            'Create & init a Smarteam session
            mySession = New SmApplic.SmSession()
            mySession.Init(SmarteamEngine, "MyApplication", myConfigName)
            mySession.OpenDatabaseConnection(SmarteamEngine.Databases(0).Alias, _
                mySmarteamDbPwd, PasswordIsEncoded:=True)

            Dim IsSmarteamAuthenticated As Boolean = mySession.UserLogin(StUsername, StPassword)
            If Not IsSmarteamAuthenticated Then
                Throw New ApplicationException("Smarteam login failed.")
            End If

            Dim SmDefaultSessionUtil As SmUtil.SmSessionUtil = mySession.GetService("SmUtil.SmSessionUtil")
            Dim SmViewOperation As SmApplic.ISmOperation = mySession.MetaInfo.AllOperations(False).ItemByName("View")

            'Read a test object from the database
            Dim STDocument As SmApplic.ISmObject = mySession.ObjectStore.RetrieveObject(ClassID, ObjectID)
            If STDocument IsNot Nothing Then
                'Check permissions by Smarteam Security Model
                Dim IsAuthorized As Boolean = SmDefaultSessionUtil.OperationAllowedOnObjectAndAuthorized(STDocument, SmViewOperation, False)
                If Not IsAuthorized Then
                    Console.WriteLine("The document is not authorized to the current Smarteam login.")
                Else
                    Dim ViewFileName = SmDefaultSessionUtil.GetViewFileName(STDocument)
                    STDocument.CopyFileFromVaultPermission(ViewFileName, tempFolder, SmApplic.FileModeEnum.modReadOnly)
                    Dim ViewFilePath As String = tempFolder + "\" + ViewFileName
                    If System.IO.File.Exists(ViewFilePath) Then
                        Console.WriteLine("File '" + ViewFileName + "' sucessfully copied from the vault.")
                    End If
                End If
            End If

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            If mySession IsNot Nothing Then
                mySession.Close()
            End If
            If SmarteamEngine IsNot Nothing Then
                SmarteamEngine.Terminate()
            End If
        End Try

        Console.ReadKey()
    End Sub

The example is for a console application but I managed to put it on a web server for web application by creating the Engine and the Default Session in Global_asax.Application_Start().

From time to time, Smarteam API hanged up at session login. Our Smarteam administrator suggested to change the settings “Logger Enabled” from ‘true’ to ‘false’ in C:\Smarteam\bin\Logger.config file.  But the problem seems to be persistent. This sis not only a problem in API programming. Smarteam Desktop client too have similar hanging problem.

Advertisements
One Comment leave one →
  1. September 27, 2014 11:44 PM

    I am using V5-6 R2013. I am trying the code with Visual Studio 2008. When i debug it fails at Smarteam engine object. Do i need to upgrade the visual studio to 2012?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: