Assign file to existing Item

Assign file to existing Item

mateusz_baczewski
Advocate Advocate
128 Views
6 Replies
Message 1 of 7

Assign file to existing Item

mateusz_baczewski
Advocate
Advocate

Hi,

 

I have the following problem: I’m trying to attach a file to an existing item. The code below executes without errors, and I can see from the date and time that the item was edited, but the file I specified was not attached. What could be causing this issue? I should add that the file I want to attach does exist.

 

 if (gridFileId != 0)
 {
     File file1 = mVault.WebServiceManager.DocumentService.GetFileById(gridFileId);
     Item item = mVault.WebServiceManager.ItemService.GetLatestItemByItemNumber("SIATKA N6");

     if (item != null)
     {
         var filter = ItemFileLnkTypOpt.Primary |
                      ItemFileLnkTypOpt.PrimarySub |
                      ItemFileLnkTypOpt.Secondary |
                      ItemFileLnkTypOpt.SecondarySub |
                      ItemFileLnkTypOpt.StandardComponent |
                      ItemFileLnkTypOpt.Tertiary;


         ItemFileAssoc[] assocByItem = mVault.WebServiceManager.ItemService
             .GetItemFileAssociationsByItemIds(new long[] { item.Id }, filter);

         var existingLinks = assocByItem?.ToList() ?? new List<ItemFileAssoc>();

         var primary = existingLinks.FirstOrDefault(a => a.Typ == ItemFileLnkTyp.Primary);
         var isPrimarySub = existingLinks.Any(a => a.Typ == ItemFileLnkTyp.PrimarySub);

         var secondaries = existingLinks
             .Where(a => a.Typ == ItemFileLnkTyp.Secondary)
             .Select(a => a.CldFileId)
             .ToList();

         var standardComponents = existingLinks
             .Where(a => a.Typ == ItemFileLnkTyp.StandardComponent)
             .Select(a => a.CldFileId)
             .ToList();

         var secondarySubs = existingLinks
             .Where(a => a.Typ == ItemFileLnkTyp.SecondarySub)
             .Select(a => a.CldFileId)
             .ToList();

         var tertiaries = existingLinks
             .Where(a => a.Typ == ItemFileLnkTyp.Tertiary)
             .Select(a => a.CldFileId)
             .ToList();

         if (!secondaries.Contains(gridFileId))
         {
             secondaries.Add(gridFileId);
         }

         long primaryFileId = primary?.CldFileId ?? 0;

         var editItem = mVault.WebServiceManager.ItemService.EditItems(new long[] { item.RevId }).FirstOrDefault();

         var updatedItem = mVault.WebServiceManager.ItemService.UpdateItemFileAssociations(
             editItem.RevId,
             primaryFileId,
             isPrimarySub,
             secondaries.ToArray(),
             standardComponents.ToArray(),
             secondarySubs.ToArray(),
             tertiaries.ToArray()
         );


         mVault.WebServiceManager.ItemService.UpdateAndCommitItems(new Item[] { updatedItem });

     }
If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes
Accepted solutions (1)
129 Views
6 Replies
Replies (6)
Message 2 of 7

Markus.Koechl
Autodesk
Autodesk

Hi @mateusz_baczewski, I understand you would like to assign your file as primary, correct? In this case, I suggest using the 

PromoteComponents() method instead. For this approach, the target Item (existing) must have the Equivalence property assigned and filled; you can manually try the behavior settings before you do it programmatically.


Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
0 Likes
Message 3 of 7

mateusz_baczewski
Advocate
Advocate

Hi @Markus.Koechl, no i would like to assing secondary file to item.

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes
Message 4 of 7

Markus.Koechl
Autodesk
Autodesk

Adding new files to an existing Item PromoteComponents() will automatically create a secondary link if a primary one exists. So, I'd prefer the same approach of leveraging a matching equivalence value for all link types. If your files to promote have documentation files, or design representation files (new in 2026 Update1) they will link (documentation) or attach (design representation) in the same process.

Anyway, if you have good reasons to follow your approach, the code should share a working sample to update associations (the sample moves secondary links to the standard component link type)

                    Item item = mVault.ItemService.GetLatestItemByItemNumber("ISO 10788-2 - 60 x 40 x 4");
                    item = mVault.ItemService.EditItems(new long[] { item.RevId }).FirstOrDefault();
                    ItemFileAssoc itemPrimAssoc = mVault.ItemService.GetItemFileAssociationsByItemIds(new long[] { item.Id }, ItemFileLnkTypOpt.Primary).FirstOrDefault();
                    ItemFileAssoc[] itemSecAssocs = mVault.ItemService.GetItemFileAssociationsByItemIds(new long[] { item.Id }, ItemFileLnkTypOpt.Secondary);
                    ItemFileAssoc[] itemStdAssocs = mVault.ItemService.GetItemFileAssociationsByItemIds(new long[] { item.Id }, ItemFileLnkTypOpt.StandardComponent);
                    List<long> secFileIds = itemSecAssocs.Select(n => n.CldFileId).ToList<long>();
                    List<long> stdFileIds = itemStdAssocs.Select(n => n.CldFileId).ToList<long>();
                    stdFileIds.AddRange(secFileIds);
                    long[] empty = new long[0];
                    item = mVault.ItemService.UpdateItemFileAssociations(item.RevId, itemPrimAssoc.CldFileId, false, empty, stdFileIds.ToArray(), empty, empty);
                    mVault.ItemService.UpdateAndCommitItems(new Item[] { item });

I hope this helps; I did not step through in detail your code - but I guess there is a minor (nasty) mismatch in the transfer of existing to new Ids.



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
0 Likes
Message 5 of 7

mateusz_baczewski
Advocate
Advocate

Hi @Markus.Koechl ,

 

I really have no idea why this is happening. I cannot assign a file to an already existing item that already has a primary file. I would like to add to the item a file whose part number is the same as the item name. When I do this from the application level or using "Assign/Update item," everything works correctly, but when I try to assign it programmatically, the code executes properly without any errors, I can see that the item "TESTOWY_ITEM" was edited, but no new file was added as secondary.

 

When I try to add it this way, a new item with a strange name gets created. Most likely this happens because the vault does not allow adding two items with the same name.

 

 File file = FindFileBySearchCondidtions(vaultConnection, "PRO-000190406.ipt");
 long fileID = file.Id;

 if (fileID != 0)
 {
     Item item = vaultConnection.WebServiceManager.ItemService.GetLatestItemByItemNumber("TESTOWY_ITEM");
     if (item != null)
     {
         item = vaultConnection.WebServiceManager.ItemService.EditItems(new long[] { item.RevId }).FirstOrDefault();

         vaultConnection.WebServiceManager.ItemService.AddFilesToPromote(
             new long[] { fileID },
             ItemAssignAll.Default,
             false
         );

         DateTime timestamp;
         GetPromoteOrderResults promoteOrder = vaultConnection.WebServiceManager.ItemService.GetPromoteComponentOrder(out timestamp);

         vaultConnection.WebServiceManager.ItemService.PromoteComponents(timestamp, promoteOrder.PrimaryArray);

         ItemsAndFiles promoteResults = vaultConnection.WebServiceManager.ItemService.GetPromoteComponentsResults(timestamp);
         vaultConnection.WebServiceManager.ItemService.UpdateAndCommitItems(promoteResults.ItemRevArray);
     }
}

mateusz_baczewski_1-1755507317538.png

 

 

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes
Message 6 of 7

Markus.Koechl
Autodesk
Autodesk
Accepted solution

Use the PromoteComponentLinks() method to assign secondary or documentation links. I added a sample to my collection: Vault-API-C#-Samples/Items/API-ConsoleApp-PromoteFileToItem/Program.cs. The sample requires two components with matching part number values. You can run the code once to create both link types in one go, or skip adding the secondary file in the first run and add it in the subsequent execution.

I hope this helps.



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
Message 7 of 7

mateusz_baczewski
Advocate
Advocate

Hi @Markus.Koechl 

Fantastic, this is exactly what I needed! Everything is working perfectly now. Many thanks!

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes