Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I attempting to programmatically update the Checked By and Approved By properties pre lifecycle change.
I've managed to create something that checks current LC and next LC, and then trigger SetVaultProperty that shall do the property changes.
But I'm completely stuck on how to update/keep the properties.
Currently I'm able to check the file out, update the property, check the file in. But during Check In the property changes is reverted.
LifeCycleStateEvent
private void UpdateFileLifecycleStateEvents_Pre(object sender, UpdateFileLifeCycleStateCommandEventArgs e)
{
// Get the service context from sender
IWebService service = sender as IWebService;
if (service == null)
{
MessageBox.Show("Service is null", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Create credentials and manager for this session
WebServiceCredentials cred = new WebServiceCredentials(service);
using (WebServiceManager mgr = new WebServiceManager(cred))
{
for (int i = 0; i < e.FileMasterIds.Length; i++)
{
long masterId = e.FileMasterIds[i];
try
{
var file = mgr.DocumentService.GetLatestFileByMasterId(masterId);
if (file == null)
{
MessageBox.Show($"No file found for Master ID: {masterId}", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
var currentState = file.FileLfCyc.LfCycStateName;
long toStateId = e.ToStateIds[i];
var lfcDefId = file.FileLfCyc.LfCycDefId;
var lfcDef = mgr.LifeCycleService.GetLifeCycleDefinitionsByIds(new long[] { lfcDefId }).FirstOrDefault();
var toState = lfcDef?.StateArray.FirstOrDefault(s => s.Id == toStateId);
var targetStateName = toState?.DispName ?? "(unknown)";
if (currentState == "For Checking" && targetStateName == "For Approval")
{
SetVaultProperty(mgr, file, "Checked By", cred.Session.User.Name);
}
else if (currentState == "For Approval" && targetStateName == "Approved")
{
SetVaultProperty(mgr, file, "Mfg Approved By", cred.Session.User.Name);
}
else if (currentState == "Work In Progress" && targetStateName == "For Approval")
{
SetVaultProperty(mgr, file, "Checked By", "-");
}
else if (currentState == "Work In Progress" && targetStateName == "Approved")
{
SetVaultProperty(mgr, file, "Checked By", "-");
SetVaultProperty(mgr, file, "Mfg Approved By", cred.Session.User.Name);
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception: {ex.Message}\n{ex.StackTrace}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
SetVaultProperty (updating the properties, but these are lost during checkin)
private void SetVaultProperty(WebServiceManager mgr, File file, string propertyName, string value)
{
MessageBox.Show($"Setting property '{propertyName}' to '{value}' for file '{file.Name}' (ID: {file.Id})", "Vault Lifecycle Extension", MessageBoxButtons.OK, MessageBoxIcon.Information);
var propDefs = mgr.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE");
var targetDef = propDefs.FirstOrDefault(p => p.DispName.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
MessageBox.Show($"Property definition found: {targetDef?.DispName ?? "None"}", "Vault Lifecycle Extension", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (targetDef != null)
{
string localPath = System.IO.Path.Combine(@"C:\Vault", file.Name);
ByteArray downloadTicket;
// Checkout the file
mgr.DocumentService.CheckoutFile(
file.Id,
CheckoutFileOptions.Master,
Environment.MachineName,
localPath,
"Checked out to update properties during Lifecycle State Transition",
out downloadTicket
);
MessageBox.Show($"File '{file.Name}' checked out for property update.", "Vault Lifecycle Extension", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Update the property
var propArray = new PropInstParamArray
{
Items = new PropInstParam[] { new PropInstParam { PropDefId = targetDef.Id, Val = value } }
};
mgr.DocumentService.UpdateFileProperties(new long[] { file.MasterId }, new PropInstParamArray[] { propArray });
// Check in the file using the upload ticket
mgr.DocumentService.CheckinUploadedFile(
file.MasterId,
"Checked in via Admin Tools",
false, // keepCheckedOut
DateTime.Now, // lastWrite
null, // associations
null, // bom
false, // copyBom
file.Name, // newFileName
file.FileClass, // fileClassification
false, // hidden
null // uploadticket
);
MessageBox.Show($"File '{file.Name}' checked in after property update.", "Vault Lifecycle Extension", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"Property definition not found for '{propertyName}'.", "Vault Lifecycle Extension", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Thanks in advance for any help.
Solved! Go to Solution.