public static void replace_files(this OT_Document document, string singledoc_newfilepath = null, string[] multidoc_newfilespath = null) { Vault vault = document.objVerEx.Vault; var obj = document.objVerEx; bool isSingleFile = vault.ObjectOperations.IsSingleFileObject(obj.ObjVer); //singlefile document if (isSingleFile && string.IsNullOrEmpty(singledoc_newfilepath)) { throw new Exception("No file supplied for single-file document."); } //multifile document if (!isSingleFile && (multidoc_newfilespath == null || multidoc_newfilespath.Length == 0)) { throw new Exception("No file(s) supplied for multi-file document."); } ObjectVersion new_version = vault.ObjectOperations.CheckOut(obj.ObjID); try { ObjVerEx new_versionex = new ObjVerEx(vault, new_version); SourceObjectFiles files = new SourceObjectFiles(); if (isSingleFile) { FileInfo fi = new FileInfo(singledoc_newfilepath); files.AddFile(fi.Name.Replace(fi.Extension, ""), fi.Extension.Replace(".", ""), fi.FullName); //change the file type IEnumerator obfile_enumerator = vault.ObjectFileOperations.GetFiles(new_version.ObjVer).GetEnumerator(); obfile_enumerator.MoveNext(); ObjectFile single_fileobj = obfile_enumerator.Current as ObjectFile; if (single_fileobj == null) { throw new Exception("Single file for this document not found."); } vault.ObjectFileOperations.RenameFile(new_versionex.ObjVer, single_fileobj.FileVer, fi.Name.Replace(fi.Extension, ""), fi.Extension.Replace(".", ""), false); } else { foreach (string file in multidoc_newfilespath) { FileInfo fi = new FileInfo(file); files.AddFile(fi.Name.Replace(fi.Extension, ""), fi.Extension.Replace(".", ""), fi.FullName); } } new_versionex.ReplaceFiles(files); new_versionex.CheckIn(); document.objVerEx = new_versionex; } catch { if (new_version.ObjectCheckedOut) { vault.ObjectOperations.ForceUndoCheckout(new_version.ObjVer); } throw; } }
public static bool update(this IObjVerEx obj, bool returnUpdatedMetadata = true, bool skipVafCalculation = false, int?modifiedby_userid = null, string[] replace_files = null) { if (obj == null) { return(false); } // Check if the objcet is deleted or destroyed if (obj.objVerEx.Deleted() || obj.objVerEx.IsDestroyed) { return(false); } ObjectVersionAndProperties checkedInObject = obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, true); if (checkedInObject.VersionData.ObjectCheckedOut) { return(false); } // bool checkedOutByVAF = false; ObjectVersion objVersion = null; PropertyValue prop_modifiedby = null; try { prop_modifiedby = obj.objVerEx.GetProperty(PD_Last_modified_by.id); objVersion = obj.objVerEx.Vault.ObjectOperations.CheckOut(obj.objVerEx.ObjID); checkedOutByVAF = true; // PropertyValues propsToUpdate = obj.objVerEx.Properties.Clone(); if (checkedInObject.Properties.Exists(PD_Comment.id) && obj.objVerEx.Properties.Exists(PD_Comment.id) && obj.objVerEx.VersionComment == checkedInObject.Properties.GetProperty(PD_Comment.id).GetValueAsUnlocalizedText()) { propsToUpdate.Remove(propsToUpdate.IndexOf(PD_Comment.id)); } if (skipVafCalculation) { string keyword = string.Empty; if (propsToUpdate.TryGetProperty(26, out PropertyValue kw)) { keyword = kw.GetValueAsUnlocalizedText(); } propsToUpdate.SetProperty(26, MFDataType.MFDatatypeText, $"{keyword}SKIP_VAF_CALCULATIONS"); } ObjectVersionAndProperties objVerAndProps = obj.objVerEx.Vault.ObjectPropertyOperations.SetAllProperties(objVersion.ObjVer, false, propsToUpdate); if (prop_modifiedby != null) { if (modifiedby_userid != null) { prop_modifiedby.TypedValue.SetValue(MFDataType.MFDatatypeLookup, modifiedby_userid); } objVerAndProps = obj.objVerEx.Vault.ObjectPropertyOperations.SetLastModificationInfoAdmin(objVersion.ObjVer, true, prop_modifiedby.TypedValue, false, null); } //replace files if (replace_files?.Length > 0 && objVersion.ObjVer.Type == (int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument) { SourceObjectFiles objFiles = new SourceObjectFiles(); foreach (string file_name in replace_files) { FileInfo fi = new FileInfo(file_name); string title = fi.Name.Replace(fi.Extension, ""); string ext = fi.Extension.Replace(".", ""); objFiles.AddFile(title, ext, fi.FullName); if (objVersion.SingleFile) { IEnumerator fileEnum = objVersion.Files.GetEnumerator(); fileEnum.MoveNext(); obj.objVerEx.Vault.ObjectFileOperations.RenameFile(objVersion.ObjVer, (fileEnum.Current as ObjectFile).FileVer, title, ext, false); break; } } ObjVerEx checkedOutVerEx = new ObjVerEx(obj.objVerEx.Vault, objVersion); checkedOutVerEx.ReplaceFiles(objFiles); } objVersion = obj.objVerEx.Vault.ObjectOperations.CheckIn(objVerAndProps.ObjVer); checkedOutByVAF = false; obj.objVerEx.ObjVer.Version = objVersion.ObjVer.Version; // if (returnUpdatedMetadata) { objVerAndProps = obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, false); obj.objVerEx = new ObjVerEx(obj.objVerEx.Vault, objVerAndProps); obj.ClearAllCachedProperties(); } } catch (Exception ex) { throw ex; } finally { if (objVersion != null && checkedOutByVAF) { // Undo checkout and enforces the operation. obj.objVerEx.Vault.ObjectOperations.ForceUndoCheckout(objVersion.ObjVer); } } return(true); }