Update associated file of Vault Item to the latest version

By Wayne Brill

In Vault Explorer you can use the Update command to make the version of an associated file the latest version.

image

This C# example shows how this update is done through the API using ItemService.PromoteComponents().

You can test this code by adding a button to this SDK sample:

“C:Program Files (x86)AutodeskAutodesk Vault 2014 SDKVS10CSharpItemEditor”

private void button2_Click(object sender, EventArgs e)    {        if (m_selectedItem == null)        {            MessageBox.Show("Select an Item first");            return;        }             long[] itemRevisionIds = new long[1];        itemRevisionIds[0] = m_selectedItem.RevId;             Item[] itemsToCommit = new Item[0];         long[] itemsToCommit_Ids = new long[0];        try        {                      ItemService itemSvc =                m_connection.WebServiceManager.ItemService;         itemSvc.UpdatePromoteComponents(itemRevisionIds);                 DateTime now = DateTime.Now;            long[] compO =                 itemSvc.GetPromoteComponentOrder(out now);                 ArrayList compOrder = new ArrayList(compO);            ArrayList subSet = new ArrayList();            int setSize = 100;            int setNum = 0;                 //get full sets            while (setNum < compOrder.Count / setSize)            {                subSet = compOrder.GetRange                              (setNum * setSize, setSize);                itemSvc.PromoteComponents              (now, (long[])subSet.ToArray(typeof(long)));                setNum++;            }                 //get remaining set            if (compOrder.Count % setSize > 0)            {                subSet = compOrder.GetRange            (setNum * setSize, compOrder.Count % setSize);                itemSvc.PromoteComponents              (now, (long[])subSet.ToArray(typeof(long)));            }                 ItemsAndFiles result = itemSvc.                         GetPromoteComponentsResults(now);            Item[] items = null;            items = result.ItemRevArray;            int[] statusArray = result.StatusArray;            // loop through the Items in the ItemRevArray            for (int i = 0; i < items.Length; i++)            {                // see if the item in the ItemRevArray                 //has been upd
ated (not equal to 1)                if (statusArray[i] != 1)                {                    //change the size of the array                    Array.Resize(ref itemsToCommit,                                 itemsToCommit.Length + 1);                    //add the updated item to the array                     //that will be committed                 itemsToCommit[itemsToCommit.Length - 1]                                               = items[i];                }                      }            //commit the updated items                          itemSvc.UpdateAndCommitItems(itemsToCommit);            // Testing catch - this could cause error             // as items contains Items that may             // not need to be updated            // itemSvc.UpdateAndCommitItems(items);        }        catch        {            // get the items that need to be undone            for (int i = 0; i < itemsToCommit.Length; i++)            {                // change the size of the array                 // of Ids (long)                Array.Resize(ref itemsToCommit_Ids,                            itemsToCommit_Ids.Length + 1);                //Add the id to the array that will be                // used in UndoEditItems()                      itemsToCommit_Ids                    [itemsToCommit_Ids.Length - 1]                                   = itemsToCommit[i].Id;            }            m_connection.WebServiceManager.ItemService.                        UndoEditItems(itemsToCommit_Ids);             }    }

Comments

2 responses to “Update associated file of Vault Item to the latest version”

  1. Hi,
    first of all many thanks for this article.
    I need some help to update a Vault Extension from 2013 to 2015. I made an extension that automatically update an item to the most recent file version during the checkout_Post event of Inventor. This extension uses the old-and-removed function “UpdateFromFile”.
    With the new Vault SDK i’m not able to obtain the same result; i don’t want to made a new item revision every time due to my company restrictions.
    I’ve tried the new “UpdateItemFileAssociation” passing the unique Id coming from the Inventor CheckIn event, but in Vault the result is that the item lose his file (the primary). If i manually do an Update from the Vault client all is ok again….but the intention of this extension is exactly to do an automatic item update.
    I’m quite desperate….
    Thanks anyway in advance
    Dave

  2. Wayne Brill Avatar
    Wayne Brill

    Hi Dave,
    Please post this question on the Vault Customization forum. Also this thread seems close to what you are trying to do.
    http://forums.autodesk.com/t5/Vault-Customization/Update-Item-From-File-during-Inventor-CheckinFile/m-p/5179947#M2430
    Thanks,
    Wayne

Leave a Reply to DaveCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading