Forum Discussion

ssmith-nova's avatar
ssmith-nova
New Contributor III
9 months ago

Trying to extract a Maintenance Unit into one XML file using a BR

As the subject line says I am trying to extract a Maintenance unit using a business rule.  We are on 8.1.0.  Below is the code I am using.  I have tried to use MetadataExtract.ExtractXML and XmlExtra...
  • RobbSalzmann's avatar
    9 months ago

    Its a bit kludgy, but it should get you what you're after:
    (Change the assignment of maintUnitToExtract to the name of your maintenance unit)

    Dim isExtractAllItemsTrue = True
    Dim isExtractUniqueIdsTrue = True 
    Dim xmlOptions As New XmlExtractOptions(isExtractAllItemFalse, isExtractUniqueIdsTrue) 
    Dim xmlString As String = String.Empty
    Dim maintUnitToExtract As String = "GolfStream Charts"
    
    ' extract specified items doesn't seem to work, but we need it
    Dim extractDict As New Dictionary(Of XmlExtractItemPk, Boolean) From {
    	{New XmlExtractItemPk(XmlExtractItemType.DashboardMaintUnit, maintUnitToExtract), True}
    }	
    
    'Execute the Metadata Extract
    Using dbConnFW As DBConnInfo = BRAPi.Database.CreateFrameworkDbConnInfo(si)
    	Using dbConnApp As DBConnInfo = BRAPi.Database.CreateApplicationDbConnInfo(si)
    		xmlString = XmlExtractController.ExtractXML(dbConnFW, dbConnApp, Nothing, xmlOptions, extractDict, XmlLoadExtractType.ApplicationWorkspaces)
    	End Using
    End Using	
    
    Dim xmlDoc As XDocument = XDocument.Parse(xmlString)
    Dim extractedMaintUnitElement As XElement = xmlDoc.Descendants("maintenanceUnit").Where(Function(mu) mu.Attribute("name").Value = maintUnitToExtract).FirstOrDefault()
    
    If extractedMaintUnitElement IsNot Nothing Then
        Dim workspaceElement = extractedMaintUnitElement.Ancestors("workspace").FirstOrDefault()
        Dim newWorkspaceElement As New XElement("workspace", workspaceElement.Attributes())
        newWorkspaceElement.Add(New XElement("maintenanceUnits", extractedMaintUnitElement))
        Dim newXmlDoc As New XDocument(
            New XElement("OneStreamXF", xmlDoc.Root.Attributes(),
                New XElement("applicationWorkspacesRoot",
                	New XElement("workspaces", newWorkspaceElement)
                )
            )
        )
        newXmlDoc.Save(filePath)
    
    End If

    this is probably doable with their library methods, but I don't have time to figure it out today.  I can look again tomorrow.