Forum Discussion

NicolasArgente's avatar
NicolasArgente
Valued Contributor
9 months ago

Add Component to DB programmatically

Hi there! I have a Workspace called WK1 I have a Dashboard called DB1 I have a Component called "George" (coz, why not?) How can I programmatically add George to the DB1 dashboard in WK1 work...
  • NicolasArgente's avatar
    9 months ago

    Did it! Great thanks to usual gang : MarcusH  and RobbSalzmann . ðŸ¤—
    MarcusH , I had a look at the solution you mentioned, it helped a lot to put me on the right path!!
    Here it is : 

    #Region "Imports"
    Imports System
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Data.Common
    Imports System.Globalization
    Imports System.IO
    Imports System.Linq
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic
    Imports OneStream.Finance.Database
    Imports OneStream.Finance.Engine
    Imports OneStream.Shared.Common
    Imports OneStream.Shared.Database
    Imports OneStream.Shared.Engine
    Imports OneStream.Shared.Wcf
    Imports OneStream.Stage.Database
    Imports OneStream.Stage.Engine
    
    Imports System.Deployment.Internal
    Imports System.Security.Cryptography
    Imports System.Text
    Imports System.Text.RegularExpressions
    Imports Group = OneStream.Shared.Wcf.Group
    #End Region 'Imports
    
    Namespace OneStream.BusinessRule.Extender.zTest4
    	Public Class MainClass
    		Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As ExtenderArgs) As Object
    			Try
    				'------ UPDATE VARIABLES ------
    				Dim sWorkspace As String = "AQS Aiqos accelerators (AQS_ACC)"
    				Dim sDashboardMaintenanceUnitName As String = "Aiqos Accelerators (AQS_ACC)"
    				Dim sDashboard As String = "zTestDB"
    				Dim sComponent As String = ""
    				'------------------------------
    								
    				Dim gWorkspaceID As Guid = BRApi.Dashboards.Workspaces.GetWorkspaceIDFromName(si, False, sWorkspace)
    				Dim dashboardWcf As New Dashboards()
    				Dim gDashboardMaintenanceUnit As OneStream.Shared.Wcf.Dashboard = dashboardWcf.GetDashboardUsingName2(si, False, gWorkspaceID, sDashboardMaintenanceUnitName)
    				Dim gComponentID As Guid = Guid.Parse("1c5e1494-76a3-4165-bae0-9febe9d20afb") ''zTestBtn component
    				Dim workspace As DashboardWorkspace = dashboardWcf.GetWorkspaceUsingName(si, False, sWorkspace)
    				Dim maintUnit As DashboardMaintUnit = BRApi.Dashboards.MaintUnits.GetMaintUnit(si, False, workspace.UniqueID, sDashboardMaintenanceUnitName)
    
    				CreateDashboards(si, dashboardWcf, maintUnit, gWorkspaceID, sDashboard, gComponentID)
    
    				Return Nothing
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Function
    
    		Private Sub CreateDashboards(si As SessionInfo, ByVal dashboardWcf As Dashboards, ByVal maintUnit As DashboardMaintUnit, ByVal gWorkspaceID As Guid, ByVal sDashboard As String, ByVal gComponentID As Guid)
    			Try
    				'Get the list of embedded dashboard components in OPT
    				Dim compSummaryInfoList As List(Of DashboardCompSummaryInfo) = dashboardWcf.GetComponentsInMaintUnit(si, False, maintUnit.UniqueID)
    
    				Dim optDashboard As Dashboard = dashboardWcf.GetDashboardUsingName2(si, False, gWorkspaceID, sDashboard)
    				If optDashboard Is Nothing Then
    					Throw New Exception("Dashboard zTestDB not found.")
    				End If
    
    
    				Dim dashInfo As DashboardInfo = dashboardWcf.GetDashboardInfo(si, False, optDashboard.UniqueID)
    				Dim componentInfoList As List(Of DashboardDbrdCompMemberInfo) = dashInfo.Components
    				Dim componentNameList As New List(Of String)()
    				'Fill the list of all component names to be embedded in the dashboard
    				For Each component In componentInfoList
    					componentNameList.Add(component.Component.Name)
    				Next
    
    				'Get the IDs of the necessary components in the correct order to be embedded
    				Dim componentIDList As New List(Of Guid)()
    				For Each compName In componentNameList
    					For Each compInfo In compSummaryInfoList
    						If compInfo.Name = compName Then
    							componentIDList.Add(compInfo.UniqueID)
    						End If
    					Next
    				Next
    
    				componentIDList.Add(gComponentID)
    
    				'Embedd the necessary components in the dashboard
    				Dim embeddedComponentList As New List(Of DashboardDbrdCompMember)()
    				For Each id In componentIDList
    					Dim dashDbrdComp As New DashboardDbrdCompMember(optDashboard.UniqueID, id, 0, String.Empty, String.Empty, String.Empty, String.Empty, XFDockPosition.Left)
    					embeddedComponentList.Add(dashDbrdComp)
    				Next
    
    				dashboardWcf.SaveDashboardAndComponentMembers(si, False, optDashboard, embeddedComponentList, True)
    
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, "Unhandled Exception in CreateDashboards() function.", ex.Message, ex.InnerException))
    			End Try
    		End Sub
    
    
    	End Class
    End Namespace