Exemple #1
0
        public static void LogoutVault(string vaultGuid)
        {
            var app = new MFilesClientApplication();
            var vcs = app.GetVaultConnectionsWithGUID(vaultGuid);

            foreach (VaultConnection vc in vcs)
            {
                if (vc.IsLoggedIn())
                {
                    var v = vc.BindToVault(IntPtr.Zero, true, true);
                    try
                    {
                        v.LogOutWithDialogs(IntPtr.Zero);
                    }
                    catch
                    {
                    }
                }
            }
        }
Exemple #2
0
        public DavisInvoice()
        {
            InitializeComponent();
            //connect to mFiles
            var mFilesApp = new MFilesClientApplication();


            //open vault
            var vaultConnect = new VaultConnection();

            vaultConnect = mFilesApp.GetVaultConnectionsWithGUID("{" + Properties.Settings.Default.vaultGUID + "}").Cast <VaultConnection>().FirstOrDefault();

            var currVault = new Vault();

            currVault = vaultConnect.BindToVault(this.Handle, true, false);

            //export
            button1.Click += delegate(object sender, EventArgs e) { button1_Click(sender, e, currVault, mFilesApp); };
            //import
            button2.Click += delegate(object sender, EventArgs e) { button2_Click(sender, e, currVault, mFilesApp); };
        }
Exemple #3
0
        public bool ObjectExists(int id)
        {
            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");
            }
            var objectSearch = new MFSearchBuilder(vault);

            objectSearch.Deleted(false);
            objectSearch.ObjType(MFBuiltInObjectType.MFBuiltInObjectTypeDocument);
            objectSearch.Class((int)vault.ClassOperations.GetObjectClassIDByAlias("Class.FerretArchiveDocument"));
            objectSearch.Conditions.AddPropertyCondition(
                (int)vault.PropertyDefOperations.GetPropertyDefIDByAlias("Prop.FerretDocId"),
                MFConditionType.MFConditionTypeEqual,
                MFDataType.MFDatatypeInteger,
                id);
            var objectResults = objectSearch.Find();

            if (objectResults.Count >= 1)
            {
                return(true);
            }
            return(false);
        }
Exemple #4
0
        public static VaultConnection InitialVaultConnection(UserDto user, VaultDto vault)
        {
            var app   = new MFilesClientApplication();
            var conns = app.GetVaultConnectionsWithGUID(vault.Guid);
            //var count = conns.Count;
            var             removeConns = new List <VaultConnection>();
            VaultConnection connection  = null;

            foreach (VaultConnection vc in conns)
            {
                if (vc.NetworkAddress != vault.Server.Ip ||
                    vc.Name != vault.Name ||
                    vc.Endpoint != vault.Server.Port)
                {
                    removeConns.Add(vc);
                }
                else
                {
                    connection = vc;
                }
            }
            if (removeConns.Count > 0)
            {
                foreach (var vc in removeConns)
                {
                    app.RemoveVaultConnection(vc.Name, vc.UserSpecific);
                }
            }
            if (connection == null)
            {
                connection = new VaultConnection
                {
                    AuthType         = MFAuthType.MFAuthTypeSpecificWindowsUser,
                    AutoLogin        = false,
                    NetworkAddress   = vault.Server.Ip,
                    Endpoint         = vault.Server.Port,
                    Name             = vault.Name,
                    ServerVaultName  = vault.Name,
                    ServerVaultGUID  = vault.Guid,
                    UserName         = user.UserName,
                    Password         = user.Password,
                    Domain           = user.Domain,
                    UserSpecific     = true,
                    ProtocolSequence = "ncacn_ip_tcp"
                };
                app.AddVaultConnection(connection);
            }
            //var now = DateTime.Now;
            Vault mfVault = null;

            if (connection.IsLoggedIn())
            {
                var v = connection.BindToVault(IntPtr.Zero, true, true);
                if (v != null)
                {
                    var accountName = v.SessionInfo.AccountName;
                    var index       = accountName.IndexOf('\\');
                    var userName    = accountName.Substring(index + 1);
                    if (StringComparer.OrdinalIgnoreCase.Equals(userName, user.UserName))
                    {
                        mfVault = v;
                    }
                    else
                    {
                        v.LogOutWithDialogs(IntPtr.Zero);
                    }
                }
            }
            return(connection);
        }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <param name="vault"></param>
        /// <param name="forceLogout">是否强制退出</param>
        /// <param name="vaultName">库名称</param>
        /// <returns></returns>
        public static Vault GetUserVault(UserDto user, VaultDto vault, bool forceLogout, string vaultName)
        {
            log.Info(" GetUserVault username="******",vault=" + vault.Name);
            if (VaultGuids.Contains(vault.Guid))
            {
                return(GetVault(user, vault.Guid));
            }
            vaultName = vaultName ?? vault.Name;
            var app         = new MFilesClientApplication();
            var removeConns = new List <VaultConnection>();

            try
            {
                var sameVC = app.GetVaultConnection(vault.Name);
                var needR  = Need2Remove(vault, sameVC, vaultName);
                if (needR)
                {
                    removeConns.Add(sameVC);
                }
            }
            catch (Exception ex)
            {
                log.Info(string.Format("GetUserVault error:{0},{1}", vaultName, ex.Message));
            }
            var conns = app.GetVaultConnectionsWithGUID(vault.Guid);

            VaultConnection connection = null;

            foreach (VaultConnection vc in conns)
            {
                var needR = Need2Remove(vault, vc, vaultName);
                if (needR)
                {
                    removeConns.Add(vc);
                }
                else
                {
                    connection = vc;
                }
            }
            if (removeConns.Count > 0)
            {
                foreach (var vc in removeConns)
                {
                    app.RemoveVaultConnection(vc.Name, vc.UserSpecific);
                }
            }
            if (connection == null)
            {
                connection = new VaultConnection
                {
                    AuthType         = MFAuthType.MFAuthTypeSpecificWindowsUser,
                    AutoLogin        = false,
                    NetworkAddress   = vault.Server.Ip,
                    Endpoint         = vault.Server.Port,
                    Name             = vaultName,
                    ServerVaultName  = vault.Name,
                    ServerVaultGUID  = vault.Guid,
                    UserName         = user.UserName,
                    Password         = user.Password,
                    Domain           = user.Domain,
                    UserSpecific     = true,
                    ProtocolSequence = "ncacn_ip_tcp"
                };
                if (String.IsNullOrEmpty(user.Domain))
                {
                    connection.AuthType = MFAuthType.MFAuthTypeSpecificMFilesUser;
                }
                app.AddVaultConnection(connection);
            }
            //var now = DateTime.Now;
            Vault mfVault = null;

            if (connection.IsLoggedIn())
            {
                var v = connection.BindToVault(IntPtr.Zero, true, true);
                if (v != null)
                {
                    if (forceLogout)
                    {
                        try
                        {
                            v.LogOutWithDialogs(IntPtr.Zero);
                        }
                        catch
                        {
                            log.Info("Remote Loggin time11111: " + DateTime.Now);
                        }
                    }
                    else
                    {
                        var accountName = v.SessionInfo.AccountName;
                        var index       = accountName.IndexOf('\\');
                        var userName    = accountName.Substring(index + 1);
                        if (StringComparer.OrdinalIgnoreCase.Equals(userName, user.UserName))
                        {
                            mfVault = v;
                        }
                        else
                        {
                            try
                            {
                                v.LogOutWithDialogs(IntPtr.Zero);
                            }
                            catch
                            {
                                log.Info("Remote Loggin time 22222: " + DateTime.Now);
                            }
                        }
                    }
                }
            }
            log.Info("Remote Loggin time: " + DateTime.Now);

            try
            {
                //now = DateTime.Now;
                var has = false;
                log.Info(string.Format(" in getuservault,userName={0}, pwd={1}, domai={2}", user.UserName, user.Password, user.Domain));
                if (forceLogout)
                {
                    mfVault = LoginVault(connection, user.UserName, user.Password, user.Domain);
                    has     = true;
                }
                if (mfVault == null || !has)
                {
                    mfVault = LoginVault(connection, user.UserName, user.Password, user.Domain);
                }
                log.Info("Loggin time: " + DateTime.Now);
            }
            catch
            {
                log.Info("Remote Loggin time: 33333" + DateTime.Now);
            }
            VaultGuids.Add(vault.Guid);
            return(mfVault);
        }
Exemple #6
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);
                }
            }
        }