private PropertyValues GetPropertyValues(CsvModel csvModel, Vault vault, int classId, ObjType objType, Dictionary<string, ObjType> objectTypeLookup, Dictionary<string, PropertyDef> propertyDefLookupByName, Dictionary<int, PropertyDef> propertyDefLookupById) { var propertyValues = new MFilesAPI.PropertyValues(); int index = 0; /* add the special class property */ propertyValues.Add(index++, GetPropertyValue((int)MFilesAPI.MFBuiltInPropertyDef.MFBuiltInPropertyDefClass, MFDataType.MFDatatypeLookup, classId)); ObjectClass objectClass = vault.ClassOperations.GetObjectClass(classId); /* if the object type has an owner */ if (objType.HasOwnerType) { /* add a special property for the owner */ var ownerObjType = vault.ObjectTypeOperations.GetObjectType(objType.OwnerType); var propertyDef = propertyDefLookupByName[ownerObjType.NameSingular]; string value; if (csvModel.Values.TryGetValue(propertyDef.Name, out value)) { object objValue = ConvertPropertyValue(propertyDef, value, vault); var propertyValue = GetPropertyValue(propertyDef.ID, propertyDef.DataType, objValue); propertyValues.Add(index++, propertyValue); } } foreach (AssociatedPropertyDef associatedPropertyDef in objectClass.AssociatedPropertyDefs) { /* get the property definition id */ PropertyDef propertyDef = PropertyDefLookupById[associatedPropertyDef.PropertyDef]; string value; if (csvModel.Values.TryGetValue(propertyDef.Name, out value)) { /* try to convert the string to the ObjectData Type */ object objValue = ConvertPropertyValue(propertyDef, value, vault); /* create the property value */ PropertyValue propertyValue = GetPropertyValue(associatedPropertyDef.PropertyDef, propertyDef.DataType, objValue); Debug.WriteLine("property - Id: {0}, Name: {1}, DataType: {2}, Value: {3}", associatedPropertyDef.PropertyDef, propertyDef.Name, propertyDef.DataType, objValue); /* add the property value */ propertyValues.Add(index++, propertyValue); } } return propertyValues; }
private void LoadToMfiles(string nameOrTitle, string documentDate, string ferretDocumentClass, string accountNumber, string clientName, string description, string extension, int docID, string filepath) { #region // Which vault are we connecting to? var vaultGuid = ((VaultComboBoxItem)vaultComboBox.SelectedItem).VaultGUID;//"{C840BE1A-5B47-4AC0-8EF7-835C166C8E24}"; // Connect to the vault using a client connection. // ref: http://developer.m-files.com/APIs/COM-API/#api-modes-client-vs-server // Note: this code will just return the first connection to the vault. var clientApplication = new MFilesClientApplication(); var vault = clientApplication .GetVaultConnectionsWithGUID(vaultGuid) .Cast <VaultConnection>() .FirstOrDefault()? .BindToVault(IntPtr.Zero, true, true); if (null == vault) { throw new NotImplementedException("Vault connection not found"); } long length = new System.IO.FileInfo("C:\\temp\\New_Sample.txt").Length; // Define the property values for the new object. var propertyValues = new MFilesAPI.PropertyValues(); // Class. var classPropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefClass }; classPropertyValue.Value.SetValue( MFDataType.MFDatatypeLookup, // This must be correct for the property definition. //(int)MFBuiltInDocumentClass.MFBuiltInDocumentClassOtherDocument (int)vault.ClassOperations.GetObjectClassIDByAlias("Class.FerretArchiveDocument") ); propertyValues.Add(-1, classPropertyValue); // Name or title. var nameOrTitlePropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefNameOrTitle }; nameOrTitlePropertyValue.Value.SetValue( MFDataType.MFDatatypeText, // This must be correct for the property definition. nameOrTitle ); propertyValues.Add(-1, nameOrTitlePropertyValue); //Douument Date var docDatePropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.DocumentDate") }; docDatePropertyValue.Value.SetValue( MFDataType.MFDatatypeDate, documentDate ); propertyValues.Add(-1, docDatePropertyValue); // Ferret Document Class var ferretDocumentClassPropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.FerretDocumentClass") }; ferretDocumentClassPropertyValue.Value.SetValue( MFDataType.MFDatatypeText, ferretDocumentClass ); propertyValues.Add(-1, ferretDocumentClassPropertyValue); // Account Number var accountNumberPropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.AccountNumber") }; accountNumberPropertyValue.Value.SetValue( MFDataType.MFDatatypeText, accountNumber ); propertyValues.Add(-1, accountNumberPropertyValue); // Client Name var clientNamePropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.ClientName") }; clientNamePropertyValue.Value.SetValue( MFDataType.MFDatatypeText, clientName ); propertyValues.Add(-1, clientNamePropertyValue); // Description var descriptionPropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.Description") }; descriptionPropertyValue.Value.SetValue( MFDataType.MFDatatypeText, description ); propertyValues.Add(-1, descriptionPropertyValue); //Ferret Doc Id var docIdPropertyValue = new MFilesAPI.PropertyValue() { PropertyDef = (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.FerretDocId") }; docIdPropertyValue.Value.SetValue( MFDataType.MFDatatypeInteger, docID ); propertyValues.Add(-1, docIdPropertyValue); // Define the source files to add. var sourceFiles = new MFilesAPI.SourceObjectFiles(); // Add one file. var myFile = new MFilesAPI.SourceObjectFile(); myFile.SourceFilePath = @"C:\temp\\New_Sample.txt"; myFile.Title = "My test document"; // For single-file-documents this is ignored. myFile.Extension = extension.Replace(".", ""); sourceFiles.Add(-1, myFile); // What object type is being created? var objectTypeID = (int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument; // A "single file document" must be both a document and contain exactly one file. var isSingleFileDocument = objectTypeID == (int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument && sourceFiles.Count == 1; #endregion // Create the object and check it in. try { if (length != 0) { var objectVersion = vault.ObjectOperations.CreateNewObjectEx( objectTypeID, propertyValues, sourceFiles, SFD: isSingleFileDocument, CheckIn: true); string msg = string.Format("Record {0}", docID); WriteLog("Success:", msg); } else { // Define the source files to add. //var sourceFiles1 = new MFilesAPI.SourceObjectFiles(); //// Add one file. //var myFile1 = new MFilesAPI.SourceObjectFile(); //myFile1.SourceFilePath = filepath; //myFile1.Title = "My test document"; // For single-file-documents this is ignored. //myFile1.Extension = extension.Replace(".", ""); //sourceFiles1.Add(-1, myFile1); //var objectVersion = vault.ObjectOperations.CreateNewObjectEx( // objectTypeID, // propertyValues, // sourceFiles1, // SFD: isSingleFileDocument, // CheckIn: true); //string msg = string.Format("Record {0}", docID); //WriteLog("Success:", msg); string ErrorMsg = string.Format("Record {0}, Document Name {1} dated {2} for account {3}. Msg is {4} ", docID, nameOrTitle, documentDate, accountNumber, "File size 0"); // e.Message); WriteLog("Error:", ErrorMsg); // string loadLog = string.Format("{0},(1},{2},{3},{4},{5},{6}", nameOrTitle, documentDate, ferretDocumentClass, accountNumber, clientName, description, filepath); // WriteErrorLog(loadLog); } } catch (System.IO.IOException e) { //try //{ // // Define the source files to add. // var sourceFiles1 = new MFilesAPI.SourceObjectFiles(); // // Add one file. // var myFile1 = new MFilesAPI.SourceObjectFile(); // myFile1.SourceFilePath = filepath; // myFile1.Title = "My test document"; // For single-file-documents this is ignored. // myFile1.Extension = extension.Replace(".", ""); // sourceFiles1.Add(-1, myFile1); // var objectVersion = vault.ObjectOperations.CreateNewObjectEx( // objectTypeID, // propertyValues, // sourceFiles1, // SFD: isSingleFileDocument, // CheckIn: true); // string msg = string.Format("Record {0}", docID); // WriteLog("Success:", msg); //} //catch { string ErrorMsg = string.Format("Record {0}, Document Name {1} dated {2} for account {3}. Msg is {4} ", docID, nameOrTitle, documentDate, accountNumber, e.Message); WriteLog("Error:", ErrorMsg); string loadLog = string.Format("{0},(1},{2},{3},{4},{5},{6}", nameOrTitle, documentDate, ferretDocumentClass, accountNumber, clientName, description, filepath); WriteErrorLog(loadLog); } } }