Vault Customization

Vault Customization

Posts: 22
Registered: ‎02-01-2012
Message 1 of 2 (166 Views)

How to find an item, a document is linked to as primary file

166 Views, 1 Replies
09-19-2012 02:15 PM

How can I find the item, when I only know the file ID of the file in the project folder (for example by

CheckinFileEvents_GetRestrictions, where the masterid of a file is given)

I suspect an item has only 1 primary linked file (is this true?) and did it by the following code (Vault 2012):


long nItemId = 0;

long nFileMasterId = 2241;   // for example

Autodesk.Connectivity.WebServices.File[] allFiles = m_serviceManager.DocumentService.GetFilesByMasterId(nFileMasterId);

if (allFiles != null && allFiles.Length > 0)


  IEnumerable<long> fileIds = from file in allFiles select file.Id;

  long[] fileIdArray = fileIds.ToArray();

  Item[] itemsAll = m_serviceManager.ItemService.GetAllLatestItems();

  if (itemsAll != null && itemsAll.Length > 0)


     IEnumerable<long> ids = from item in itemsAll select item.Id;

     long[] itemIdArray = ids.ToArray();

     ItemFileAssoc[] associations = m_serviceManager.ItemService.GetItemFileAssociationsByItemIds(itemIdArray, ItemFileLnkTypOpt.Primary);

     if (associations != null && associations.Length > 0)


        bool bFound = false;

        foreach (ItemFileAssoc assoc in associations)


           if (bFound == false)


              foreach (long id in fileIdArray)


                 if (id == assoc.CldFileId)


                    nItemId = assoc.ParItemId;

                    bFound = true;










Is this the only one possibility or is there a better way?


By the way, in my test, the linked primary file has not the newest version as you can see in the attachment.

is this a fact?

Have I to resolve the link and reassign to the newest version manually?

How to do this by Api?



Posts: 814
Registered: ‎12-12-2006
Message 2 of 2 (153 Views)

Re: How to find an item, a document is linked to as primary file

09-20-2012 05:20 AM in reply to: ewcv

Yes, there is an easier way.  Starting with the file master ID, you should be able to get what you want in 2 API calls.


1. Call GetLatestFileByMasterId to get a File object.

2. Call GetItemsByFileIdAndLinkTypeOptions passing in the ID of the File.  Even if the item was created from an older file version, this function will still find the related items.


To answer your other question:  Yes, it's true that an item will have no more than one primary link to a file.  But you don't need to use that fact in this case.





Doug Redmond
Software Engineer
Autodesk, Inc.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!