This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[COM API] Mixed result with creating Vault structure through code

Working on something fun, but having mixed results: can't get a perfect result with creating structure through code, using AddObjectTypeAdmin, AddPropertyDefAdmin, AddObjectClassAdmin.

Expected: creating an OT "Log", PD "LogMessage", a CL "Log" through code and aliases for each.
Result: an OT Log, PD LogMessage, but TWO "Log" classes (with sequential ids), where the first has become a "builtin" class (when I try to delete it from my local vault)!
See screenshot

It's a bit of a hit or miss with examples lacking for AddObjectTypeAdmin, AddPropertyDefAdmin, AddObjectClassAdmin

This is my code. What am I missing that causes two Log classes to be created?

var logObjectTypeID = vault.ObjectTypeOperations.GetObjectTypeIDByAlias("OT.Log");
var logClassID = vault.ClassOperations.GetObjectClassIDByAlias("CL.Log");
var logMessagePropDefID = vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.LogMessage");


// Add Log ObjectType if it doesn't exist
if (logObjectTypeID == -1)
{
ObjType objType = new ObjType()
{
NameSingular = "Log",
NamePlural = "Logs",
AllowAdding = false,
AllowedAsGroupingLevel = false,
RealObjectType = true,
CanHaveFiles = false,
External = false,
ShowCreationCommandInTaskPane = false,
Hierarchical = false,
HasOwnerType = false,
OwnerType = 0,
Translatable = false
};

var objTypeAdmin = new ObjTypeAdmin()
{
ObjectType = objType,
SemanticAliases = new SemanticAliases() { Value = "OT.Log" }
};

var newObjTypeAdmin = vault.ObjectTypeOperations.AddObjectTypeAdmin(objTypeAdmin);
logObjectTypeID = newObjTypeAdmin.ObjectType.ID;
}

// Add LogMessage propertyDef if it doesn't exist
if (logMessagePropDefID == -1)
{
var propDef = new PropertyDef
{
Name = structureConfig.LogMessagePropDefName,
DataType = MFDataType.MFDatatypeMultiLineText,
ContentType = MFContentType.MFContentTypeGeneric,
ObjectType = logObjectTypeID, // Either ID of the newly created ObjectType or the found-by-alias ObjectType
BasedOnValueList = false,
AllObjectTypes = false,
Predefined = false,
ObjectsSearchableByThisProperty = false,
HistoryVersionsSearchableByThisProperty = false,
AllowedAsGroupingLevel = false,
SortAscending = true,
FormattingType = MFFormattingType.MFFormattingTypeNone,
AutomaticValueType = MFAutomaticValueType.MFAutomaticValueTypeNone,
ValidationType = MFValidationType.MFValidationTypeNone,
UpdateType = MFUpdateType.MFUpdateTypeNormal,
AccessControlList = new AccessControlList(),
};

var propDefAdmin = new PropertyDefAdmin()
{
PropertyDef = propDef,
SemanticAliases = new SemanticAliases() { Value = "PD.LogMessage" }
};

var newPropertyDefAdmin = vault.PropertyDefOperations.AddPropertyDefAdmin(propDefAdmin);
logMessagePropDefID = newPropertyDefAdmin.PropertyDef.ID;
}


// Add Class if it doesn't exist
if (logClassID == -1)
{
var associatedNamePropDef = new AssociatedPropertyDef
{
PropertyDef = 0, // NameOrTitle
Required = true
};

var associatedLogMessagePropDef = new AssociatedPropertyDef
{
PropertyDef = logMessagePropDefID,
Required = false
};

var associatedPropDefs = new AssociatedPropertyDefs();
associatedPropDefs.Add(-1, associatedNamePropDef);
associatedPropDefs.Add(-1, associatedLogMessagePropDef);

var objectClassAdmin = new ObjectClassAdmin
{
Name = structureConfig.LogClassName,
ObjectType = logObjectTypeID,
NamePropertyDef = 0, // NameOrTitle PropertyDef
AssociatedPropertyDefs = associatedPropDefs,
ForceWorkflow = false,

SemanticAliases = new SemanticAliases() { Value = "CL.Log" }
};

var newClassAdmin = vault.ClassOperations.AddObjectClassAdmin(objectClassAdmin);
logClassID = newClassAdmin.ID;
}