Exemplo n.º 1
0
    private SourceObjectFiles GetSourceObjectFiles(CsvModel csvModel)
    {
      SourceObjectFiles sourceObjectFiles = null;

      var sourceFiles = csvModel.SourceFiles.Select(f => SourceFileModel.FromFile(f)).ToArray();

      Debug.WriteLine("sourceFiles.Length: " + sourceFiles.Length);


      if (sourceFiles.Length > 0)
      {
        sourceObjectFiles = new MFilesAPI.SourceObjectFiles();

        /* add a source file */
        for (int i = 0; i < sourceFiles.Length; i++)
        {
          var sourceObjectFile = new MFilesAPI.SourceObjectFile();
          sourceObjectFile.SourceFilePath = sourceFiles[i].FilePath;
          sourceObjectFile.Title = sourceFiles[i].Title;
          sourceObjectFile.Extension = sourceFiles[i].Extension;
          sourceObjectFiles.Add(i, sourceObjectFile);
        }
      }

      return sourceObjectFiles;
    }
Exemplo n.º 2
0
        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);
                }
            }
        }