Add a pdf file to a Vault Item as an attachment

Add a pdf file to a Vault Item as an attachment

PetterMoldestad
Enthusiast Enthusiast
3,590 Views
7 Replies
Message 1 of 8

Add a pdf file to a Vault Item as an attachment

PetterMoldestad
Enthusiast
Enthusiast

Hello.

 

I’ve try to write code to add a pdf file to a Vault Item as an attachment. But I struggle to get how it should be. Can I get a little help, please? I use Vault 2016 Pro and VB.net.

0 Likes
Accepted solutions (1)
3,591 Views
7 Replies
Replies (7)
Message 2 of 8

PetterMoldestad
Enthusiast
Enthusiast

Here is my try by the way:

 

   Private Sub attachPdfToItem(sItemNumber As String, lPdfFileIdInVault As Long)

       Dim oItemSvc As ACW.ItemService = m_conn.WebServiceManager.ItemService

 

       Dim oItem As ACW.Item = Nothing

       oItem = oItemSvc.GetLatestItemByItemNumber(sItemNumber)

 

       Dim lItemRevId As Long

       lItemRevId = oItem.RevId

 

       Dim attmt As New ACW.Attmt

 

       attmt.FileId = lPdfFileIdInVault

       attmt.Pin = False

 

       Dim attmts(0) As ACW.Attmt

 

       attmts(0).FileId = attmt.FileId 'This line fails.

       attmts(0).Pin = False

 

       oItemSvc.UpdateAttachments(lItemRevId, attmts)

   End Sub.

0 Likes
Message 3 of 8

PetterMoldestad
Enthusiast
Enthusiast
Accepted solution

Got it. 🙂 This works.

 

Private Sub attachPdfToItem(sItemNumber As String, lPdfFileIdInVault As Long)

       Dim oItemSvc As ACW.ItemService = m_conn.WebServiceManager.ItemService

 

       Dim oItem As ACW.Item = Nothing

       oItem = oItemSvc.GetLatestItemByItemNumber(sItemNumber)

 

       Dim lItemId As Long

       lItemId = oItem.Id

 

       Dim lItemIds(0) As Long

       lItemIds(0) = lItemId

 

       Dim lItemRevId As Long

       lItemRevId = oItem.RevId

 

       Dim lItemRevIds(0) As Long

       lItemRevIds(0) = lItemRevId

 

       Dim attmt As New ACW.Attmt

       attmt.FileId = lPdfFileIdInVault

       attmt.Pin = False

 

       Dim oAttachments As ACW.ItemAttmt()

       oAttachments = oItemSvc.GetAttachmentsByItemIds(lItemIds)

 

       Dim iNumberOfExistingItemAttms As Integer

       iNumberOfExistingItemAttms = oAttachments.Count

 

       Dim attmts() As ACW.Attmt

 

       If iNumberOfExistingItemAttms > 0 Then

           Dim iNumberOfExistingAttms As Integer

           iNumberOfExistingAttms = oAttachments(0).AttmtArray.Count

 

           ReDim Preserve attmts(0 To iNumberOfExistingAttms)

           Dim i As Integer

           For i = 0 To iNumberOfExistingAttms - 1

               attmts(i) = oAttachments(0).AttmtArray(i)

           Next

           attmts(iNumberOfExistingAttms) = attmt

       Else

           ReDim Preserve attmts(0)

           attmts(0) = attmt

       End If

 

       Dim oEditedItems() As ACW.Item

       oEditedItems = oItemSvc.EditItems(lItemRevIds)

       oItemSvc.UpdateAttachments(lItemRevId, attmts)

       oItemSvc.UpdateAndCommitItems(oEditedItems)

   End Sub

0 Likes
Message 4 of 8

Saqib.Iqbal
Enthusiast
Enthusiast

@PetterMoldestad WHAT IF I WANT TO ADD PDF AS A LINK, NOT ATTACHMENT? AND (STUPID QUESTION) WHERE AND HOW TO USE THIS CODE, I MEAN WHERE WRITE AND RUN THIS CODE IN VAULT?

0 Likes
Message 5 of 8

PetterMoldestad
Enthusiast
Enthusiast

Hello Saquib

I don't know how to add pdf links in Items in Vault. Maybe someone else can answer this.

To run this code in vault it needs to be a part of a Vault Add Ins program written in Microsoft Visual Studio. On how to write add ins in Vault I refer to the SDK setup.exe file provided with the Vault Server program. You find this file at the Vault server here.

C:\Program Files\Autodesk\ADMS Professional 2019\SDK\setup.exe

When you run this file if unpack description files, help files, samples files and things you need to write Vault add ins.

0 Likes
Message 6 of 8

ThomasRambach
Advisor
Advisor

I'm having some issues deciphering how to add a file as an attachment to an item from the code in this sample. I'm building in C#. There is no direct command to add a file as an attachment? 

0 Likes
Message 7 of 8

Markus.Koechl
Autodesk
Autodesk

It is a two-step action required: read existing attachments (could be empty), and add your new PDF file iteration to this list. Then update the item by the ItemService.UpateAttachments method.

ItemAttmt[] attachments = conn.WebServiceManager.ItemService.GetAttachmentsByItemIds(editableItem.Id.ToSingleArray());
                    List<Attmt> attachments2 = null;
                    if (attachments.Length == 1)
                    {
                        attachments2 = attachments[0].AttmtArray.ToList();
                    }
                    else
                        attachments2 = new List<Attmt>();
                    try
                    {
                        attachments2.Add(new Attmt() { FileId = addedFile.EntityIterationId, Pin = false });
                        editableItem = conn.WebServiceManager.ItemService.UpdateAttachments(editableItem.RevId, attachments2.ToArray());
                    }
                    catch
                    {
                        throw new Exception("Item's attached BOM report could not get attached. Check that items are not locked for the job user in the given state Settings.quickChangeState");
                    }

 The full context of the code including managing the item lifecycle to get an editable item can be found here: https://github.com/koechlm/Vault-Sample---Item-BOM-Report-Job 



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
Message 8 of 8

dennis
Advisor
Advisor

Excellent help for my coding with Vault 2022.

I am working with selecting a file from the Project Manager and then selecting an ITEM to attach the file.

            VDF.Vault.Currency.Connections.Connection connection = e.Context.Application.Connection;
            var selection = e.Context.CurrentSelectionSet.First();
var itemIds = new List<long> { selection.Id };
                var itemList = itemService.GetItemsByIds(itemIds.ToArray());
                var itemId = itemList[0].RevId;
                Attmt fileAttach = new Attmt() { FileId = PdfId, Pin = false }; 
                attachmentsList.Add(fileAttach);
                var result = itemService.UpdateAttachments(itemId, attachmentsList.ToArray());
0 Likes