private void btn_submit_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrWhiteSpace(txt_path.Text))
     {
         MessageBox.Show("You must select a csv before proceeding");
     }
     else
     {
         var  uploadedFile = new FileInfo(txt_path.Text);
         bool isFileLocked = FieldCreatorHelpers.IsFileLocked(uploadedFile);
         if (!isFileLocked)
         {
             ExecuteMethod(BuildAttributes);
         }
         else
         {
             MessageBox.Show("File is currently locked. Please make sure the csv is not open");
         }
     }
 }
        private void BuildAttributes()
        {
            WorkAsync(new WorkAsyncInfo
            {
                Message = "Preparing...",

                Work = (worker, args) =>
                {
                    List <Attribute> attributeList = Attribute.ReturnAttributeList(txt_path.Text);
                    Attribute.ProcessGlobalOptionSetAttributeList(worker, attributeList, Service);
                    Attribute.ProcessAttributeList(worker, attributeList, Service);
                    FieldCreatorHelpers.PublishXml(worker, Service);
                },

                ProgressChanged = (args) =>
                {
                    SetWorkingMessage($"Attributes Processed {args.ProgressPercentage.ToString()}%\n" + args.UserState);
                },

                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        lst_csvlines.DataSource = ImportLogs;
                        btn_browse.Enabled      = false;
                        btn_submit.Enabled      = false;
                        btn_export.Enabled      = true;
                        btn_refresh.Enabled     = true;
                    }
                }
            });
        }
        public static void ProcessAttributeList(BackgroundWorker worker, List <Attribute> attributeList, IOrganizationService service)
        {
            var importLogs        = new List <string>();
            var importEntities    = new List <string>();
            int successfulImports = 0;
            int failedImports     = 0;

            foreach (var attr in attributeList)
            {
                int progressComplete = FieldCreatorHelpers.ReturnProgressComplete(attributeList.IndexOf(attr), attributeList.Count());
                worker.ReportProgress(progressComplete, attr.FieldSchemaName);

                string            fullyQualFieldTypeName = AttrTypes[attr.FieldType];
                Type              attributeType          = Type.GetType(fullyQualFieldTypeName);
                dynamic           attribute         = Activator.CreateInstance(attributeType, attr);
                var               attributeInstance = (IAttribute)attribute;
                AttributeMetadata attributeMetadata = attributeInstance.ReturnAttributeMetadata(attr);
                if (attr.FieldType == "Lookup")
                {
                    CreateOneToManyRequest createOneToManyRelationshipRequest = new CreateOneToManyRequest
                    {
                        OneToManyRelationship = new OneToManyRelationshipMetadata
                        {
                            ReferencedEntity            = attr.ReferencedEntity,
                            ReferencingEntity           = attribute.AttrEntitySchemaName,
                            SchemaName                  = attr.OnetoNRelationshipSchemaName,
                            AssociatedMenuConfiguration = new AssociatedMenuConfiguration
                            {
                                Behavior = AssociatedMenuBehavior.UseLabel,
                                Group    = AssociatedMenuGroup.Details,
                                Label    = new Label(attribute.AttrEntitySchemaName, CultureInfo.CurrentCulture.LCID),
                                Order    = 10000
                            },
                            CascadeConfiguration = new CascadeConfiguration
                            {
                                Assign   = CascadeType.NoCascade,
                                Delete   = CascadeType.RemoveLink,
                                Merge    = CascadeType.NoCascade,
                                Reparent = CascadeType.NoCascade,
                                Share    = CascadeType.NoCascade,
                                Unshare  = CascadeType.NoCascade
                            }
                        },
                        Lookup             = (LookupAttributeMetadata)attributeMetadata,
                        SolutionUniqueName = attr.SolutionUniqueName
                    };
                    try
                    {
                        service.Execute(createOneToManyRelationshipRequest);
                        importLogs.Add($"Success | {attr.FieldSchemaName}");
                        importEntities.Add(attr.EntitySchemaName);
                        successfulImports++;
                    }
                    catch (Exception exception)
                    {
                        importLogs.Add($"Fail | {attr.FieldSchemaName} - {exception.Message}");
                        failedImports++;
                    }
                }
                else
                {
                    CreateAttributeRequest createAttrRequest = new CreateAttributeRequest
                    {
                        SolutionUniqueName = attribute.AttrSolution,
                        EntityName         = attribute.AttrEntitySchemaName,
                        Attribute          = attributeMetadata
                    };
                    try
                    {
                        service.Execute(createAttrRequest);
                        importLogs.Add($"Success | {attr.FieldSchemaName}");
                        importEntities.Add(attr.EntitySchemaName);
                        successfulImports++;
                    }
                    catch (Exception exception)
                    {
                        importLogs.Add($"Fail | {attr.FieldSchemaName} - {exception.Message}");
                        failedImports++;
                    }
                }
            }
            FieldCreatorPluginControl.ImportEntities = importEntities;
            FieldCreatorPluginControl.ImportLogs     = importLogs;
        }