By Barbara Han
The recommended way of adding user defined property (UDP) is using Vault Explorer UI, but sometimes the developer wants to do it through code.
AddPropertyDefinition function is the API to use for achieving that goal. It allows you to set property mapping to some file’s properties, for example iProperties in Inventor file, when creating the new UDP. To set up the mapping, the content source’s Moniker is the key information. The Moniker is a string composed by three parts:
1. The display name of the file property;
2. A CLSID for the property (i.e. the CLSID for any custom property in Inventor file can be found in Vault 2012\Explorer\ProviderConfig_Inventor.xml: Microsoft User Defined PropertySet id. The GetPropertyDefinitionInfosByEntityClassId function in Vault API can also return you all information for the property.);
3. A string indicates the data type of the property’s value.
These three parts are split by the exclamation mark. Here is an example of Moniker value for a custom property named ‘UserPropertyTest’ in Inventor file:
UserPropertyTest!{D5CDD505-2E9C-101B-9397-08002B2CF9AE}!nvarchar
The first part is usually different for different property, but the second part of the Moniker value mapping to Inventor iProperties is constant on all Vaults. This may be not true for other products. For instance, it was said that the second part for everyone of AutoCAD title block’s attribute is different, and even for a same attribute, the second part is not same on different vault server.
The following is a C# sample demonstrating how to add a UDP named “TestMapAPI” and set write (no read) mapping to map it to a custom property named “UserPropertyTest” in Inventor file. Before running it, need to have an existing UDP named “Test” in Vault and it is mapping to “UserPropertyTest” property in Inventor file.
// Set up the login credentials for Vault.
// For demonstration purposes, the information is hard-coded.
UserPasswordCredentials login = new UserPasswordCredentials(
"localhost", "Vault", "Administrator", "", false);
// Create the WebServiceManager which can be used for all Vault Server calls.
// Putting the manager in a using block insures that it logs out when we are done.
using (WebServiceManager serviceManager = new WebServiceManager(login))
{
PropDef[] props = serviceManager.PropertyService.
GetPropertyDefinitionsByEntityClassId("FILE");
string Moniker = "";
foreach (PropDef prop in props)
{
if (prop.DispName == "Test")
{
PropDefInfo[] propInfos = serviceManager.
PropertyService.GetPropertyDefinitionInfosByEntityClassId(
"FILE", new long[] { prop.Id });
foreach (PropDefInfo propInfo in propInfos)
{
Moniker = propInfo.EntClassCtntSrcPropCfgArray[0].
CtntSrcPropDefArray[0].Moniker.Split('!')[1];
}
}
}
CtntSrcPropDef ctntSrcPropDef = new CtntSrcPropDef();
ctntSrcPropDef.DispName = "UserPropertyTest";
ctntSrcPropDef.CanCreateNew = true;
ctntSrcPropDef.Classification = Classification.Custom;
ctntSrcPropDef.CtntSrcDefTyp = CSPDefTypes.File;
ctntSrcPropDef.CtntSrcId = 4;
ctntSrcPropDef.MapDirection = AllowedMappingDirection.Write;
ctntSrcPropDef.Typ = DataType.String;
ctntSrcPropDef.Moniker = "UserPropertyTest!"+Moniker+"!nvarchar";
EntClassCtntSrcPropCfg entClsCtntSrcPropCfg = new EntClassCtntSrcPropCfg();
entClsCtntSrcPropCfg.EntClassId = "FILE";
entClsCtntSrcPropCfg.CanCreateNewArray = new bool[] { true };
entClsCtntSrcPropCfg.CtntSrcPropDefArray = new CtntSrcPropDef[] {
ctntSrcPropDef };
entClsCtntSrcPropCfg.MapDirectionArray = new MappingDirection[] {
MappingDirection.Write };
entClsCtntSrcPropCfg.MapTypArray = new Autodesk.Connectivity.WebServices.
MappingType[] { Autodesk.Connectivity.WebServices.MappingType.Constant };
entClsCtntSrcPropCfg.PriorityArray = new int[] { 0 };
PropDefInfo propDefInfo = serviceManager.PropertyService.AddPropertyDefinition(
"921579AE-C383-4837-AC78-05B19F5DDDDB", "TestMapAPI", DataType.String,
false, true, string.Empty, new string[] { "FILE" },
new EntClassCtntSrcPropCfg[] { entityClassCtntSrcPropCfg }, null, null);
If you are mapping to a non-custom Inventor iProperties, e.g. “Project” property, you need to change some properties of the CtntSrcPropDef object in above code – including changing .DispName to “Project”, and .Classification to “Classification.Standard”, beside .Moniker being changed to:
"Project!" + Moniker + "!nvarchar";