Finding the Associated Files of an assembly with the Vault API

Finding the Associated Files of an assembly with the Vault API

8013068
Enthusiast Enthusiast
2,644 Views
4 Replies
Message 1 of 5

Finding the Associated Files of an assembly with the Vault API

8013068
Enthusiast
Enthusiast

I'm fairly new to programming, so bear with me...

I'm trying to to make a section of code that will find a file by searching for an assembly part number. Once the file is found, I want to return all the associated file names (the sub-assemblies and parts) like the "uses" tool in vault. I'm trying to use GetFileAssociationLites to find the related file IDs and then GetFilesByIds to return an array of all the files which I can then get the names of, but something is wrong.

It is giving me an error at the GetFilesByIds line, which I think is related to the output from GetFileAssociationLites, but I can't figure out the issue. Everything else seems to be working.

Any help is really appreciated... thanks

        'part number search term (hardcoded for example)
        Dim searchterm As String
        searchterm = "5-AIIDRI12TA-KT87SB"

        'set up search
        Dim filePropDefs As PropDef() = connection.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")
        Dim projectPropDef As PropDef = filePropDefs.[Single](Function(n) n.SysName = "PartNumber")
        Dim condition As New SrchCond() With {.PropDefId = projectPropDef.Id, .PropTyp = PropertySearchType.SingleProperty, .SrchOper = 3, .SrchRule = SearchRuleType.Must, .SrchTxt = searchterm}
        Dim bookmark As String = String.Empty
        Dim status As SrchStatus = Nothing

        'do the search and return the file(s) with the matching part number
        Dim files As File() = connection.WebServiceManager.DocumentService.FindFilesBySearchConditions(New SrchCond() {condition}, Nothing, Nothing, True, True, bookmark, status)

        'setup file relationship settings (for use in the loop below)
        Dim myFileRelationshipSettings As VDF.Vault.Settings.FileRelationshipGatheringSettings
        myFileRelationshipSettings = New VDF.Vault.Settings.FileRelationshipGatheringSettings
        myFileRelationshipSettings.IncludeAttachments = False
        myFileRelationshipSettings.IncludeChildren = True
        myFileRelationshipSettings.IncludeParents = True
        myFileRelationshipSettings.IncludeRelatedDocumentation = False

        'go through each file found with the search term
        If files IsNot Nothing AndAlso files.Any() Then
            For Each file As File In files

                'get the file ID from the file
                Dim fileid As Long
                fileid = file.Id

                'use this file ID to find the associated file IDs and store in an array
                Dim relatedfileids As Array
                relatedfileids = connection.FileManager.GetFileAssociationLites(New Long() {fileid}, myFileRelationshipSettings)

                'use the related file IDs to find each associated file and store in an array
                Dim relatedfiles As Array
                relatedfiles = connection.WebServiceManager.DocumentService.GetFilesByIds(relatedfileids) '********** This line is giving me the error **********

                '(testing only) send each associated file name to a message box to check what's going on
                Dim i As Long
                For i = 0 To UBound(relatedfiles)
                    MsgBox(relatedfiles(i).name)
                Next

            Next
        End If






0 Likes
Accepted solutions (1)
2,645 Views
4 Replies
Replies (4)
Message 2 of 5

wangdu
Collaborator
Collaborator

Hi,

 

The FileManager.GetFileAssociationLites method gives you back IEnumerable<FileAssocLite> and so you can't just pass them to the GetFilesByIds method. That would explain why you are getting the error on GetFilesByIds method.

As a side note, check the results and see if that's what you are expecting, otherwise the settings for IncludeParents might not be necessary.

 

Hope it helps!

 

Wangdu

 

coolOrange

www.coolOrange.com

0 Likes
Message 3 of 5

8013068
Enthusiast
Enthusiast

Thank you, that's really helpful. For some reason, I thought GetFileAssociationLites returned an array of file IDs. Do you how I can use the IEnumerable<FileAssocLite> file to get the file Ids?

0 Likes
Message 4 of 5

wangdu
Collaborator
Collaborator
Accepted solution

Hi,

 

ParFileId and CldFileId properties are file ids within this FileAssocLite class that you need to use depending on your settings passed in the parameter. In your case, the CldFileId property probably is the correct one.

 

Hope it helps!

 

Wangdu

 

coolOrange

www.coolOrange.com

0 Likes
Message 5 of 5

8013068
Enthusiast
Enthusiast

Thanks for the help. I've gotten some code to work. I'm sure it could be better, but here it is for anyone else struggling with this. Also, this was a good resource: http://adndevblog.typepad.com/manufacturing/2013/09/vault-2014-api-example-that-adds-a-file-and-asso...

        'setup file relationship settings (for use in the loop below)
        Dim myFileRelationshipSettings As VDF.Vault.Settings.FileRelationshipGatheringSettings
        myFileRelationshipSettings = New VDF.Vault.Settings.FileRelationshipGatheringSettings
        myFileRelationshipSettings.IncludeAttachments = False
        myFileRelationshipSettings.IncludeChildren = True
        myFileRelationshipSettings.IncludeParents = False
        myFileRelationshipSettings.IncludeRelatedDocumentation = False

        'go through each file found with the search term
        If files IsNot Nothing AndAlso files.Any() Then
            For Each file As File In files

                'get the file ID from the file
                Dim fileid As Long
                fileid = file.Id

                'find the file associations
                Dim relatedfileassoc = GV.connectionx.FileManager.GetFileAssociationLites(New Long() {fileid}, myFileRelationshipSettings)
                'GV.connectionx

                'go through each association
                Dim fileassoc As FileAssocLite
                For Each fileassoc In relatedfileassoc
                    'get the related files and show their names
                    Dim relatedfile As File
                    relatedfile = GV.connectionx.WebServiceManager.DocumentService.GetFileById(fileassoc.CldFileId)
                    MsgBox(relatedfile.Name)
                Next
            Next

        End If
0 Likes