Import XML to create objects

ssmith-nova
New Contributor II

Hi All, 

I have a process that exports a dashboard and all of its components to XML.  It then reads through the XML and creates new objects.  The process works when I import the XML by hand.  I was wondering if there is a way to have the code:

1) delete objects if they exist

2) import the XML to create the new objects. 

 

Thank you,
Scott

7 REPLIES 7

MarcusH
Contributor III

The only method I know that will effectively replace Dashboards is XFProject. Have a look in the Design and Reference Guide and search this forum for more info. This screenshot gives you an idea:

MarcusH_0-1711380465932.png

 

ssmith-nova
New Contributor II

I had looked at that.  Do you know if there a method to import and export the xfProj via code?  

 

XFProj has its own XML library: XFProjectWcf

Use XFProjectWcf.ExtractXFProjectFileHierarchy to extract,  and XFProjectWcf.StartLoadXFProjectFileHierarchy using similar structure to the code posted for application zip and dashboards.

and here is some code I've used in the past, maybe it can help you get started:

Using dbconnfw As DbConnInfo = BRApi.Database.CreateFrameworkDbConnInfo(si)
	Using dbconnApp As DbConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)
		'Get the solution code 3 digit identifier 
			Dim solutionCode As String = BRApi.Dashboards.Parameters.GetLiteralParameterValue(si, False, "HelpAboutSolutionCode_AMGH")
		'Inzialize the zip object
		Dim xfprojObj As New XFProject()
		xfprojObj.DefaultZipFileName = $"{datetime.Now:yyyyMMddHHmm}_{solutionCode}"
		xfprojObj.TopFolderPath = solutionCode
		
		'OBJECTS LIST
		
		'Business Rules
		Dim businessRulesList As List(Of String) = BRApi.Database.ExecuteSql(dbconnapp, $"SELECT DISTINCT Name FROM BusinessRule WHERE Name LIKE '%{solutionCode}%'", False).AsEnumerable().Select(Function(lambda) lambda.item("Name").tostring()).tolist()
		For Each brName As String In businessRulesList
			Dim xfProjItem As New XFProjectItem(xfprojectitemtype.BusinessRule,"",brName,True)
			xfprojObj.ProjectItems.Add(xfProjItem)
		Next brName
		
		'Dashboard Maintenance unit
		Dim dashboardMaintUnitList As New List(Of String) From {$"OpenPlace Template ({solutionCode})"}
		For Each dsMaintUnit As String In dashboardMaintUnitList
			Dim xfProjItem As New XFProjectItem(xfprojectitemtype.DashboardMaintenanceUnit,"",dsMaintUnit,True)
			xfprojObj.ProjectItems.Add(xfProjItem)
		Next dsMaintUnit
		
		'Dashboard Profile
		Dim dashboardProfilesList As New List(Of String) From {$"OpenPlace Template ({solutionCode})"}
		For Each dsProfile As String In dashboardProfilesList
			Dim xfProjItem As New XFProjectItem(xfprojectitemtype.DashboardProfile,"",dsProfile,True)
			xfprojObj.ProjectItems.Add(xfProjItem)
		Next dsProfile

		'Data Management Jobs
		Dim dmGroupsList As New List(Of String) From {$"Export Solution ({solutionCode})"}
		For Each dmGroup As String In dmGroupsList
			Dim xfProjItem As New XFProjectItem(xfprojectitemtype.DataManagementGroup,"",dmGroup,True)
			xfprojObj.ProjectItems.Add(xfProjItem)
		Next dmGroup
		
		'Process the export
		Dim zipByte As Byte() = XfProjectWcf.ExtractXFProjectZipFile(dbconnfw,dbconnApp,xfprojObj)
		Dim targetFileInfo As New XFFileInfo(filesystemlocation.ApplicationDatabase,path.Combine("Documents","Users",stringhelper.RemoveSystemCharacters(si.UserName,False,False),xfprojObj.DefaultZipFileName & ".zip"))
		Dim targetFile As New XFFile(targetFileInfo,String.Empty,zipByte)
		brapi.FileSystem.InsertOrUpdateFile(si, targetFile)
		
		'Extract the xfProject xml file
		Dim targetXFProjInfo As New XFFileInfo(filesystemlocation.ApplicationDatabase,path.Combine("Documents","Users",stringhelper.RemoveSystemCharacters(si.UserName,False,False),xfprojObj.DefaultZipFileName & ".xfproj"))
		Dim targetXFProjFile As New XFFile(targetXFProjInfo,String.Empty,xfprojObj.WriteXmlStringAsBytes(si))
		brapi.FileSystem.InsertOrUpdateFile(si, targetXFProjFile)
		
	End Using
End Using

 

In your example, what is the "solutionCode"? I looks like it is a Parameter called  HelpAboutSolutionCode_AMGH.

what is {$"OpenPlace Template ({solutionCode})"} and $"Export Solution ({solutionCode})"}

SolutionCode is  a three letter designation that is stored in a Literal Parameter, specific to the solution.  
You can omit it in the code if you're not using a Solution Code.  Omitting it will simply bring back everything in your application, instead of things specific to a solution.

RobbSalzmann
Valued Contributor

You would use the XmlLoadWcf library for this.  It can load either xml as a string or as a zip file.  It runs similar to running a Data Management sequence, returning a TaskActivity object:

						
Dim task As TaskActivityItem = Nothing
'Execute the XML load and assign the TaskActivityItem
task = XmlLoadWcf.StartLoadXml(si, xmlString, Nothing,  String.Empty, SystemXmlFileType.Unknown, ApplicationXmlFileType.ApplicationDashboards, Nothing)


Here it is an example round trip using some of @PeterFu's code:

Dim configSettings as AppServerConfigSettings = AppServerConfig.GetSettings(si)
Dim folderPath as String = FileShareFolderHelper.GetDataManagementExportUsernameFolderForApp(si, True, configSettings.FileShareRootFolder, si.AppToken.AppName) & "\" & DateTime.UtcNow.ToString("yyyyMMdd") & "\MetadataExtracts"
if Not Directory.Exists(folderPath) then Directory.CreateDirectory(folderPath)
Dim filePath as String = folderPath & "\" & si.AppToken.AppName & ".zip"				
If File.Exists(filePath) Then File.Delete(filePath)

'Set the extract options
Dim xmlOptions as New XmlExtractOptions
xmlOptions.ExtractAllItems = True

'Execute the Metadata Extract
Using dbConnFW as DBConnInfo = BRAPi.Database.CreateFrameworkDbConnInfo(si)
	Using dbConnApp as DBConnInfo = BRAPi.Database.CreateApplicationDbConnInfo(si)
		Dim zipBytes as Byte() = ApplicationZipFileHelper.Extract(dbConnFW, dbConnApp, Nothing, xmlOptions)
		'Append the contents of this workflow profile to the extract file			            
		Using FS As New FileStream(filePath, FileMode.Append, FileAccess.Write)			
			'Create a binary writer, and write all bytes to the FileStream at once
            Using BW As New BinaryWriter(FS)
                BW.Write(zipBytes)
            End Using
		End Using
	End Using
End Using

'Load the file back in
Dim task As TaskActivityItem = Nothing

'load the application zip file
task = XmlLoadWcf.StartLoadXml(si, "",  Nothing, filePath, SystemXmlFileType.Unknown, ApplicationXmlFileType.ApplicationZipFile, Nothing)