public bool Create(CustomFieldModel model)
        {
            if (model == null)
            {
                throw new ArgumentException("The CustomFieldModel is null.");
            }
            else
            {
                if (GetEntities().Any(item => item.Name == model.Name))
                {
                    // No need to do anything currently
                }
                else
                {
                    LookupTable table = null;
                    switch (model.InputType.ToLower())
                    {
                    case "lookuptable":
                        table         = new LookupTableCSOMClient().GetEntities().FirstOrDefault(item => item.Name == model.LookupTable);
                        model.Formula = null;
                        break;

                    case "freeform":
                        model.Formula = null;
                        break;
                    }

                    CustomFieldCreationInformation customFieldInfo = new CustomFieldCreationInformation()
                    {
                        Id          = Guid.NewGuid(),
                        EntityType  = GetEntityType(model.EntityType),
                        FieldType   = model.ValueType.ConvertToCustomFieldType(),
                        LookupTable = table,
                        Formula     = model.Formula,
                        Name        = model.Name
                    };

                    // CSOM API doesn't support to create CustomFiled using multi threads
                    lock (CustomFieldCSOMClient.strLock)
                    {
                        bool result = ExcuteJobWithRetries(() =>
                        {
                            LoggerHelper.Instance.Comment("About to Create CustomField with Name: " + customFieldInfo.Name);
                            CustomField field = BaseProjectContext.CustomFields.Add(customFieldInfo);
                            BaseProjectContext.CustomFields.Update();
                            BaseProjectContext.ExecuteQuery();
                            LoggerHelper.Instance.Comment("Finish Creating CustomField with Name: " + customFieldInfo.Name);
                        },
                                                           "Create CustomField");
                        return(result);
                    }
                }
            }
            return(true);
        }
        public async System.Threading.Tasks.Task WhenAddAsync_IfNotEntityTypeIsDefined_ThenArgumentNullExceptionIsThrown()
        {
            // ARRANGE
            var customFieldCreationInformation = new CustomFieldCreationInformation()
            {
                Id   = Guid.NewGuid(),
                Name = NamePrefix + Guid.NewGuid().ToString()
            };

            // ACT & Assert
            await Assert.ThrowsAsync <ArgumentNullException>(() => _client.AddAsync(customFieldCreationInformation));
        }
        public async System.Threading.Tasks.Task WhenAddAsync_ThenCustomFieldIsReturned()
        {
            // ARRANGE
            var customFieldCreationInformation = new CustomFieldCreationInformation()
            {
                Id        = Guid.NewGuid(),
                Name      = NamePrefix + Guid.NewGuid().ToString(),
                FieldType = CustomFieldType.TEXT
            };

            // ACT
            var publishedProject = await _client.AddAsync(customFieldCreationInformation);

            // ASSERT
            Assert.NotNull(publishedProject);
        }
        public async System.Threading.Tasks.Task WhenGetByNameAsync_IfExistCustomFieldWithRightName_ThenIsReturned()
        {
            // ARRANGE
            var creationInformation = new CustomFieldCreationInformation()
            {
                Id        = Guid.NewGuid(),
                Name      = NamePrefix + Guid.NewGuid().ToString(),
                FieldType = CustomFieldType.TEXT
            };
            await _client.AddAsync(creationInformation);

            // ACT
            var result = await _client.GetByNameAsync(creationInformation.Name);

            // ASSERT
            Assert.Equal(creationInformation.Name, result.Name);
        }
        public async System.Threading.Tasks.Task WhenGetByEntityTypeAsync_IfExistCustomFieldsWithRightType_ThenAreReturned()
        {
            // ARRANGE
            var entityType          = (await _entityTypeClient.GetAllAsync()).TaskEntity;
            var creationInformation = new CustomFieldCreationInformation()
            {
                Id         = Guid.NewGuid(),
                Name       = NamePrefix + Guid.NewGuid().ToString(),
                FieldType  = CustomFieldType.TEXT,
                EntityType = entityType
            };
            await _client.AddAsync(creationInformation);

            // ACT
            var result = await _client.GetAllByEntityTypeAsync(entityType);

            // ASSERT
            Assert.True(result.All(cf => cf.EntityType.ID == entityType.ID));
        }
        private void CreateCustomFields()
        {
            EntityType      entityType          = ProjContext.EntityTypes.ProjectEntity;
            CustomFieldType customFieldType     = CustomFieldType.TEXT;
            LookupTable     selectedLookupTable = null;
            bool            allowMultiSelect    = false;

            CB_EntityType.InvokeIfRequired(cb => entityType = cb.SelectedValue as EntityType);
            CB_Type.InvokeIfRequired(sb => customFieldType  = (CustomFieldType)CB_Type.SelectedValue);
            CB_LookupTable.InvokeIfRequired(cb =>
            {
                if (cb.SelectedIndex <= 0)
                {
                    return;
                }
                selectedLookupTable = cb.SelectedValue as LookupTable;
                if (CBX_AllowMultiSelect.Checked)
                {
                    allowMultiSelect = true;
                }
            });
            for (int customFieldCount = 1; customFieldCount <= Convert.ToInt32(NUD_CFNumber.Value); customFieldCount++)
            {
                CustomFieldCreationInformation cfCi = new CustomFieldCreationInformation
                {
                    Name       = TB_Name.Text + customFieldCount,
                    EntityType = entityType,
                    FieldType  = customFieldType
                };
                Log.WriteVerbose(new SourceInfo(), TB_Status, "Creating custom field with name {0}", cfCi.Name);
                if (selectedLookupTable != null)
                {
                    cfCi.LookupTable = selectedLookupTable;
                    Log.WriteVerbose(new SourceInfo(), TB_Status, "Setting custom field to use lookup table {0}", selectedLookupTable.Name);
                }

                cfCi.LookupAllowMultiSelect = allowMultiSelect;
                ProjContext.CustomFields.Add(cfCi);
            }
            ProjContext.CustomFields.Update();
            ProjContext.ExecuteQuery();
        }
        public async Task <CustomField> AddAsync(CustomFieldCreationInformation creationInformation)
        {
            if (creationInformation == null)
            {
                throw new ArgumentNullException(nameof(creationInformation));
            }

            if (!Enum.IsDefined(typeof(CustomFieldType), creationInformation.FieldType))
            {
                throw new ArgumentNullException(nameof(creationInformation.FieldType));
            }

            CustomField customField = null;

            try
            {
                // Ensure no other custom field with same id or name exist
                _projectContext.Load(_projectContext.CustomFields);
                var customFields = _projectContext.LoadQuery(_projectContext.CustomFields.Where(pr =>
                                                                                                pr.Id == creationInformation.Id || pr.Name == creationInformation.Name));
                await _projectContext.ExecuteQueryAsync();

                if (!customFields.Any())
                {
                    _projectContext.CustomFields.Add(creationInformation);

                    _projectContext.CustomFields.Update();

                    customField = await GetByIdAsync(creationInformation.Id);
                }
            }
            catch (Exception ex)
            {
                // TODO: LOG ERROR!

                throw new CsomClientException($"Unexcepted error adding a new custom field. " +
                                              $"Project context url is {_projectContext.Url}.", ex);
            }

            return(customField);
        }
        public async System.Threading.Tasks.Task WhenRemoveAsync_ThenCustomFieldIsRemoved()
        {
            // ARRANGE
            var customFieldCreationInformation = new CustomFieldCreationInformation()
            {
                Id        = Guid.NewGuid(),
                Name      = NamePrefix + Guid.NewGuid().ToString(),
                FieldType = CustomFieldType.TEXT
            };

            // ACT
            var customField = await _client.AddAsync(customFieldCreationInformation);

            var result = await _client.RemoveAsync(customField);

            customField = await _client.GetByIdAsync(customFieldCreationInformation.Id);

            // ASSERT
            Assert.True(result);
            Assert.Null(customField);
        }
        private void CreateCustomFields()
        {
            EntityType entityType = ProjContext.EntityTypes.ProjectEntity;
            CustomFieldType customFieldType = CustomFieldType.TEXT;
            LookupTable selectedLookupTable = null;
            bool allowMultiSelect = false;

            CB_EntityType.InvokeIfRequired(cb => entityType = cb.SelectedValue as EntityType);
            CB_Type.InvokeIfRequired(sb => customFieldType = (CustomFieldType)CB_Type.SelectedValue);
            CB_LookupTable.InvokeIfRequired(cb =>
                   {
                       if (cb.SelectedIndex <= 0) return;
                       selectedLookupTable = cb.SelectedValue as LookupTable;
                       if (CBX_AllowMultiSelect.Checked)
                           allowMultiSelect = true;
                   });
            for (int customFieldCount = 1; customFieldCount <= Convert.ToInt32(NUD_CFNumber.Value); customFieldCount++)
            {
                CustomFieldCreationInformation cfCi = new CustomFieldCreationInformation
                {
                    Name = TB_Name.Text + customFieldCount,
                    EntityType = entityType,
                    FieldType = customFieldType
                };
                Log.WriteVerbose(new SourceInfo(), TB_Status, "Creating custom field with name {0}", cfCi.Name);
                if (selectedLookupTable != null)
                {
                    cfCi.LookupTable = selectedLookupTable;
                    Log.WriteVerbose(new SourceInfo(), TB_Status, "Setting custom field to use lookup table {0}", selectedLookupTable.Name);
                }

                cfCi.LookupAllowMultiSelect = allowMultiSelect;
                ProjContext.CustomFields.Add(cfCi);
            }
            ProjContext.CustomFields.Update();
            ProjContext.ExecuteQuery();
        }
        public async System.Threading.Tasks.Task WhenAddAsync_IfNameExists_ThenNullIsReturned()
        {
            // ARRANGE
            var customFieldCreationInformation = new CustomFieldCreationInformation()
            {
                Id        = Guid.NewGuid(),
                Name      = NamePrefix + Guid.NewGuid().ToString(),
                FieldType = CustomFieldType.TEXT
            };
            var customFieldCreationInformation2 = new CustomFieldCreationInformation()
            {
                Id        = Guid.NewGuid(),
                Name      = customFieldCreationInformation.Name,
                FieldType = CustomFieldType.TEXT
            };

            // ACT
            await _client.AddAsync(customFieldCreationInformation);

            var customFieldSecondTime = await _client.AddAsync(customFieldCreationInformation2);

            // ASSERT
            Assert.Null(customFieldSecondTime);
        }