Change Item Number through Vault API

By Barbara Han

Issue
I want to assign an item number of my own (and not use the numbering scheme item number) to the existing item or the new item that I added. How to do that with Vault API?

Solution
To use the custom Item number, the custom Item number needs to be added into Vault first through AddItemNumbers.

The following sample code demonstrates how to change the item number of an existing item. For the new item, just replace the EditItem function in below code with AddItemRevision function. AddItemRevision function also makes the item editable.

C#

ItemService itemSvc = m_serviceManager.ItemService;

Item editableItem = itemSvc.EditItem(m_selectedItem.RevId);

 

NumSchm[] numSchms;

numSchms = itemSvc.GetNumberingSchemesByType(NumSchmType.Activated);

 

System.Collections.ArrayList numArr = new System.Collections.ArrayList();

 

foreach (NumSchm _numSchm in numSchms)

{

  if (_numSchm.Name == "Mapped") numArr.Add(_numSchm.SchmID);

}

 

long[] numSchmIds = (long[])numArr.ToArray(typeof(long));

 

long[] masterIds = new long[1];

masterIds[0] = editableItem.MasterId;

 

string[] newItemNum;

newItemNum = new string[] { "blabla" };

StringArray[] fieldInputs = new StringArray[1];

StringArray tempArr = new StringArray();

tempArr.Items = newItemNum;

fieldInputs[0] = tempArr;

 

ItemNum[] newNumbers = itemSvc.AddItemNumbers(masterIds, numSchmIds, fieldInputs);

 

editableItem.ItemNum = newNumbers[0].ItemNum1;

 

//commit the item, which finalizes the object

 

Item[] items = new Item[1];

items[0] = editableItem;

itemSvc.UpdateAndCommitItems(items);

VB.NET

Dim itemSvc As ItemService = m_serviceManager.ItemService

Dim editableItem As Item = itemSvc.EditItem(m_selectedItem.RevId)

 

‘To chang the item number, the item number must exist in Vault

‘ use AddItemNumbers to add the item number

 

Dim numSchms() As NumSchm

numSchms = itemSvc.GetNumberingSchemesByType(NumSchmType.Activated)

 

Dim numArr As ArrayList

numArr = New ArrayList

Dim _numSchm As NumSchm

 

For Each _numSchm In numSchms

  If (_numSchm.Name = "Mapped") Then numArr.Add(_numSchm.SchmID)

Next

 

Dim numSchmIds() As Long = CType(numArr.ToArray(GetType(System.Int64)), Long())

 

Dim masterIds(0) As Long

masterIds(0) = editableItem.MasterId

 

‘ Change the item number to "blabla"

 

Dim newItem() As String

newItem = New String() {"blabla"}

 

Dim fieldInputs(0) As StringArray

Dim tempArr As New StringArray

tempArr.Items = newItem

 

fieldInputs(0) = tempArr

 

Dim numbers As ItemNum() = itemSvc.AddItemNumbers(masterIds, numSchmIds, fieldInputs)

editableItem.ItemNum = numbers(0).ItemNum1

 

‘ commit the item, which finalizes the object

Dim items(0) As Item

items(0) = editableItem

itemSvc.UpdateAndCommitItems(items)

End Sub

<p>Please refer to the below code snippet if you want to use AddItemRevision function:</p>  <div style="font-family:;background: white;color:">   <p style="margin: 0px"><span style="line-height: 11pt;color:"><font face="Courier New"><font style="font-size: 8pt" color="#008000">// Get the Part category - just an example,</font></font></span></p>    <p style="margin: 0px"><span style="line-height: 11pt;color:"><font face="Courier New"><font style="font-size: 8pt" color="#008000">// you can chose other categories</font></font></span></p>    <p style="margin: 0px"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160;</font></font></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt;color:"><font color="#2b91af"><font style="font-size: 8pt">Cat</font></font></span><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000">[] categories = ServiceManager.CategoryService.</font></span></font></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt"><font color="#000000"><font style="font-size: 8pt">&#160; GetCategoriesByEntityClassId(</font></font></span><font style="font-size: 8pt"><span style="line-height: 11pt;color:"><font color="#a31515">&quot;ITEM&quot;</font></span><span style="line-height: 11pt"><font color="#000000">, </font></span><span style="line-height: 11pt;color:"><font color="#0000ff">true</font></span></font><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000">);</font></span></font></p>    <p style="margin: 0px"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160;</font></font></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt;color:"><font color="#0000ff"><font style="font-size: 8pt">long</font></font></span><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000"> catId = -1;</font></span></font></p>    <p style="margin: 0px"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160;</font></font></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt;color:"><font color="#0000ff"><font style="font-size: 8pt">foreach</font></font></span><font style="font-size: 8pt"><span style="line-height: 11pt"><font color="#000000"> (</font></span><span style="line-height: 11pt;color:"><font color="#2b91af">Cat</font></span><span style="line-height: 11pt"><font color="#000000"> category </font></span><span style="line-height: 11pt;color:"><font color="#0000ff">in</font></span></font><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000"> categories)</font></span></font></p>    <p style="margin: 0px"><span style="line-height: 11pt"><font face="Courier New"><font style="font-size: 8pt" color="#000000">{</font></font></span></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt"><font color="#000000"><font style="font-size: 8pt">&#160; </font></font></span><font style="font-size: 8pt"><span style="line-height: 11pt;color:"><font color="#0000ff">if</font></span><span style="line-height: 11pt"><font color="#000000"> ((category.SysName == </font></span><span style="line-height: 11pt;color:"><font color="#a31515">&quot;Part&quot;</font></span></font><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000">))</font></span></font></p>    <p style="margin: 0px"><span style="line-height: 11pt"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160; {</font></font></span></p>    <p style="margin: 0px"><span style="line-height: 11pt"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160;&#160;&#160; catId = category.Id;</font></font></span></p>    <p style="margin: 0px"><span style="line-height: 11pt"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160; }</font></font></span></p>    <p style="margin: 0px"><span style="line-height: 11pt"><font face="Courier New"><font style="font-size: 8pt" color="#000000">}</font></font></span></p>    <p style="margin: 0px"><font face="Courier New"><font style="font-size: 8pt" color="#000000">&#160;</font></font></p>    <p style="margin: 0px"><font face="Courier New"><span style="line-height: 11pt;color:"><font color="#2b91af"><font style="font-size: 8pt">Item</font></font></span><span style="line-height: 11pt"><font style="font-size: 8pt" color="#000000"> editableItem = ServiceManager.ItemService.AddItemRevision(catId);</font></span></font></p> </div>

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading