The ItemService.UpdateAndCommitItem function can add, modify and remove the custom properties on an item, so we now see how to use it. The UserDefinedProperties argument of this function decides what custom properties apply to the item.
The following is a lightweight C# sample demonstrating how to add a custom property to an item:
using System;
using System.Collections;
using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;
namespace ItemAttachment
{
class App
{
private WebServiceManager ServiceManager;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
App app = new App();
app.Run();
}
catch (System.Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
}
}
private void Run()
{
Login();
Item item = GetItem();
try
{
// get all the available properties
PropDef[] propdefs = ServiceManager.ItemService.GetAllItemPropertyDefinitions();
long[] ids = new long[propdefs.Length];
long idsi = 0;
// store their ids in a list
foreach (PropDef propdef in propdefs)
ids[idsi++] = propdef.Id;
// get all the properties for this item
PropInst[] oldprops = ServiceManager.ItemService.GetItemProperties(new long[] { item.Id }, ids);
// store it in a list, so that we can easiliy add to it
ArrayList props = new ArrayList();
if (oldprops != null)
props = new ArrayList(oldprops);
// create new property item
PropInst prop = new PropInst();
prop.EntityId = item.Id;
prop.PropDefId = propdefs[0].Id;
prop.ValTyp = propdefs[0].Typ;
prop.Val = "blabla";
// add it to the list
props.Add(prop);
// Get the file associations for later we need
// to pass the existing file associations back to the item
ItemFileAssoc[] assocs = ServiceManager.ItemService.
GetItemFileAssociationsByItemIds(new long[] { item.Id },
ItemFileLnkTypOpt.Primary | ItemFileLnkTypOpt.Secondary |
ItemFileLnkTypOpt.StandardComponent |
ItemFileLnkTypOpt.PrimarySub | ItemFileLnkTypOpt.SecondarySub);
long primaryId = 0;
bool isPrimarySubComp = false;
ArrayList secondaryList = new ArrayList();
ArrayList stdCompList = new ArrayList();
ArrayList secSudCompList = new ArrayList();
foreach (ItemFileAssoc assoc in assocs)
{
if (assoc.Typ == ItemFileLnkTyp.Primary)
primaryId = assoc.CldFileId;
if (assoc.Typ == ItemFileLnkTyp.PrimarySub)
isPrimarySubComp = true;
if (assoc.Typ == ItemFileLnkTyp.Secondary)
secondaryList.Add(assoc.CldFileId);
if (assoc.Typ == ItemFileLnkTyp.StandardComponent)
stdCompList.Add(assoc.CldFileId);
if (assoc.Typ == ItemFileLnkTyp.SecondarySub)
secSudCompList.Add(assoc.CldFileId);
}
long[] secondary = (long[])secondaryList.ToArray(typeof(long));
long[] stdComp = (long[])stdCompList.ToArray(typeof(long));
long[] secSudComp = (long[])secSudCompList.ToArray(typeof(long));
Attmt[] atts = ServiceManager.ItemService.GetAttachmentsByItemId(item.Id);
ServiceManager.ItemService.UpdateAndCommitItem(item, primaryId, isPrimarySubComp,secondary,stdComp,
secSudComp, atts, (PropInst[])props.ToArray(typeof(PropInst)), 4);
Console.WriteLine("Item {0} was succesfully updated.",
item.ItemNum);
Console.ReadLine();
}
catch
{
ServiceManager.ItemService.UndoEditItems(new long[] { item.Id });
Console.WriteLine("Error while editing item.");
}
Logout();
}
private void Login()
{
Console.Write("User Name: ");
string userName = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
Console.Write("Server: ");
string server = Console.ReadLine();
Console.Write("Vault: ");
string vault = Console.ReadLine();
UserPasswordCredentials cred = new UserPasswordCredentials(server, vault, userName, password);
ServiceManager = new WebServiceManager(cred);
}
private void Logout()
{
ServiceManager.Dispose();
}
private Item GetItem()
{
Console.Write("Create new item [Y/N] <Yes>: ");
string answer = Console.ReadLine();
if (0 == answer.Length)
{
answer = "Y";
}
Item item = null;
if (0 == string.Compare(answer, "Y", true))
{
// Get the Part category - just an example,
// you can chose other categories
Cat[] categories = ServiceManager.CategoryService.
GetCategoriesByEntityClassId("ITEM", true);
long catId = -1;
foreach (Cat category in categories)
{
if ((category.SysName == "Part"))
{
catId = category.Id;
}
}
item = ServiceManager.ItemService.AddItemRevision(catId);
}
else
{
Console.Write("Enter item number: ");
string itemNo = Console.ReadLine();
Item itemRev = ServiceManager.ItemService.GetLatestItemByItemNumber(itemNo);
item = ServiceManager.ItemService.EditItem(itemRev.RevId);
}
return item;
}
private ArrayList GetFiles()
{
ArrayList results = new ArrayList();
do
{
Console.Write("Enter full document name (incl. path): ");
string filePath = Console.ReadLine();
if (0 == filePath.Length)
{
break;
}
File[] files = ServiceManager.DocumentService.FindLatestFilesByPaths(new string[] { filePath });
foreach (File file in files)
{
if (-1 == file.Id)
{
continue;
}
results.Add(file);
}
} while (true);
return results;
}
}
}