private void CreateNNRelation(string entityLogicalName, AttributeTemplate attributeTemplate)
        {
            string entity1Logical, entity2Logical;
            var displayNames = attributeTemplate.DisplayName.Split(';');
            var entity1Display = displayNames.First();
            var entity2Display = displayNames.Last();
            if (attributeTemplate.Description == "1")
            {
                entity1Logical = attributeTemplate.LogicalName;
                entity2Logical = attributeTemplate.LookupEntityLogicalName;
            }
            else
            {
                entity1Logical = attributeTemplate.LookupEntityLogicalName;
                entity2Logical = attributeTemplate.LogicalName;
            }

            AssociatedMenuBehavior? entity1Behavior = entity1Display == entity1Logical ? AssociatedMenuBehavior.DoNotDisplay : AssociatedMenuBehavior.UseLabel;
            AssociatedMenuBehavior? entity2Behavior = entity2Display == entity2Logical ? AssociatedMenuBehavior.DoNotDisplay : AssociatedMenuBehavior.UseLabel;

            var createManyToManyRelationshipRequest =
                new CreateManyToManyRequest
                {
                    IntersectEntitySchemaName = attributeTemplate.LogicalName,
                    ManyToManyRelationship = new ManyToManyRelationshipMetadata
                    {
                        SchemaName = attributeTemplate.LogicalName,
                        Entity1LogicalName = entity1Logical,
                        Entity1AssociatedMenuConfiguration =
                        new AssociatedMenuConfiguration
                        {
                            Behavior = entity1Behavior,
                            Group = AssociatedMenuGroup.Details,
                            Label = new Label(entity1Display, 1033),
                            Order = 10000
                        },
                        Entity2LogicalName = entity2Logical,
                        Entity2AssociatedMenuConfiguration =
                        new AssociatedMenuConfiguration
                        {
                            Behavior = entity2Behavior,
                            Group = AssociatedMenuGroup.Details,
                            Label = new Label(entity2Display, 1033),
                            Order = 10000
                        }
                    }
                };

            ExecuteOperation(GetSharedOrganizationService(), createManyToManyRelationshipRequest,
                string.Format("An error occured while creating the NN: {0}",
                    attributeTemplate.LogicalName));
        }
 private PicklistAttributeMetadata CreateOptionSetAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var optionMetadataCollection = GetOptionMetadataCollection(attributeTemplate);
     var picklistAttributeMetadata = new PicklistAttributeMetadata
     {
         OptionSet = new OptionSetMetadata(optionMetadataCollection)
     };
     picklistAttributeMetadata.OptionSet.IsGlobal = false;
     return picklistAttributeMetadata;
 }
 private MoneyAttributeMetadata CreateMoneyAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var moneyAttributeMetadata = new MoneyAttributeMetadata
     {
         PrecisionSource = attributeTemplate.Precision == default(int)
             ? DefaultConfiguration.DefaultMoneyPrecisionSource
             : attributeTemplate.Precision
     };
     return moneyAttributeMetadata;
 }
 private MemoAttributeMetadata CreateMultilineAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var stringAttributeMetadata = new MemoAttributeMetadata
     {
         MaxLength = attributeTemplate.MaxLength == default(int)
             ? DefaultConfiguration.DefaultMemoMaxLength
             : attributeTemplate.MaxLength,
         Format = StringFormat.TextArea
     };
     return stringAttributeMetadata;
 }
        private AttributeTemplate GetAttributeTemplate(Range xlsRange, int currentRow, string logicalName,
            string excelFile, List<Exception> errorList, List<Exception> warningList)
        {
            var displayName = GetCellValueAsString(xlsRange, currentRow, attributeDisplayNameColumn);
            var displayNameShort = displayName.Length > DefaultConfiguration.AttributeDisplayNameMaxLength
                ? displayName.Substring(0, DefaultConfiguration.AttributeDisplayNameMaxLength)
                : displayName;
            var minLength = default(int);
            var maxLength = default(int);
            var minLengthStr = GetCellValueAsString(xlsRange, currentRow, attributeMinValueColumn);
            var maxLengthStr = GetCellValueAsString(xlsRange, currentRow, attributeMaxValueColumn);
            maxLengthStr = maxLengthStr.Replace(".", "").Replace(",",".");
            var isRequiredStr = GetCellValueAsString(xlsRange, currentRow, attributeRequiredColumn).ToLower();
            var isRequired = false;// DefaultConfiguration.YesKeywordList.Contains(isRequiredStr);

            if (!string.IsNullOrWhiteSpace(maxLengthStr) && !int.TryParse(maxLengthStr, out maxLength))
            {
                maxLength = default(int);
            }
            minLengthStr = minLengthStr.Replace(".", "").Replace(",",".");

            if (!string.IsNullOrWhiteSpace(minLengthStr) && !int.TryParse(minLengthStr, out minLength))
            {
                minLength = default(int);
            }
            var attributeType =
                CommonHelper.GetAttributeType(GetCellValueAsString(xlsRange, currentRow, attributeAttributeTypeColumn),
                    excelFile, errorList, warningList);
            if (attributeType == typeof (Exception))
            {
                return null;
            }

            var attributeTemplate = new AttributeTemplate
            {
                LogicalName = logicalName,
                DisplayName = displayName,
                DisplayNameShort = displayNameShort,
                Description = GetCellValueAsString(xlsRange, currentRow, attributeDescriptionColumn),
                MinLength = minLength,
                MaxLength = maxLength,
                AttributeType = attributeType,
                IsRequired = isRequired,
                OtherDisplayName = GetCellValueAsString(xlsRange, currentRow, attributeOtherDisplayNameColumn),
                OtherDescription = GetCellValueAsString(xlsRange, currentRow, attributeOtherDescriptionColumn)
            };

            attributeTemplate.LogicalName = attributeTemplate.LogicalName.Trim();
            attributeTemplate.DisplayName = attributeTemplate.DisplayName.Trim();

            return attributeTemplate;
        }
        private void CreateLookupAttribute(string entityLogicalName, AttributeTemplate attributeTemplate)
        {
            var schemaName = attributeTemplate.LogicalName + "_" +
                        attributeTemplate.LookupEntityLogicalName + "_" +
                        entityLogicalName;
            if(schemaName.Length > 100)
            {
                schemaName = schemaName.Substring(default(int), 100);
            }

            var createOneToManyRequest = new CreateOneToManyRequest
            {
                Lookup = new LookupAttributeMetadata
                {
                    Description = GetLabelWithLocalized(attributeTemplate.Description),
                    DisplayName = GetLabelWithLocalized(attributeTemplate.DisplayNameShort),
                    LogicalName = attributeTemplate.LogicalName,
                    SchemaName = attributeTemplate.LogicalName,
                    RequiredLevel = new AttributeRequiredLevelManagedProperty(attributeTemplate.IsRequired
                        ? AttributeRequiredLevel.SystemRequired
                        : AttributeRequiredLevel.None)
                },
                OneToManyRelationship = new OneToManyRelationshipMetadata
                {
                    ReferencedEntity = attributeTemplate.LookupEntityLogicalName,
                    ReferencingEntity = entityLogicalName,
                    SchemaName = schemaName
                }
            };

            if (!string.IsNullOrWhiteSpace(attributeTemplate.OtherDisplayName))
            {
                var otherDisplayLabel = new LocalizedLabel(attributeTemplate.OtherDisplayName, DefaultConfiguration.OtherLanguageCode);
                createOneToManyRequest.Lookup.DisplayName.LocalizedLabels.Add(otherDisplayLabel);
            }

            if (!string.IsNullOrWhiteSpace(attributeTemplate.OtherDescription))
            {
                var otherDescriptionLabel = new LocalizedLabel(attributeTemplate.OtherDescription, DefaultConfiguration.OtherLanguageCode);
                createOneToManyRequest.Lookup.Description.LocalizedLabels.Add(otherDescriptionLabel);
            }

            CreateRequest createWebRequest = null;
            if (attributeTemplate.DisplayName.Length > DefaultConfiguration.AttributeDisplayNameMaxLength)
            {
                createWebRequest = GetCreateWebResourceRequest(entityLogicalName, attributeTemplate);
            }

            ExecuteOperation(GetSharedOrganizationService(), createOneToManyRequest,
                string.Format("An error occured while creating the attribute: {0}",
                    attributeTemplate.LogicalName));

            if (createWebRequest != null)
            {
                ExecuteOperation(GetSharedOrganizationService(), createWebRequest,
                    string.Format("An error occured while creating the web resource for attribute: {0}",
                        attributeTemplate.LogicalName));
            }
        }
        private void CreateGlobalOptionSetAttribute(AttributeTemplate attributeTemplate)
        {
            var optionMetadataCollection = GetOptionMetadataCollection(attributeTemplate);
            var createOptionSetRequest = new CreateOptionSetRequest
            {
                OptionSet = new OptionSetMetadata(optionMetadataCollection)
                {
                    Name = attributeTemplate.GlobalOptionSetListLogicalName,
                    DisplayName = GetLabelWithLocalized(attributeTemplate.DisplayNameShort),
                    Description = GetLabelWithLocalized(attributeTemplate.Description),
                    IsGlobal = true,
                    OptionSetType = OptionSetType.Picklist
                }
            };

            if (!string.IsNullOrWhiteSpace(attributeTemplate.OtherDisplayName))
            {
                var otherDisplayLabel = new LocalizedLabel(attributeTemplate.OtherDisplayName, DefaultConfiguration.OtherLanguageCode);
                createOptionSetRequest.OptionSet.DisplayName.LocalizedLabels.Add(otherDisplayLabel);
            }

            if (!string.IsNullOrWhiteSpace(attributeTemplate.OtherDescription))
            {
                var otherDescriptionLabel = new LocalizedLabel(attributeTemplate.OtherDescription, DefaultConfiguration.OtherLanguageCode);
                createOptionSetRequest.OptionSet.Description.LocalizedLabels.Add(otherDescriptionLabel);
            }

            ExecuteOperation(GetSharedOrganizationService(), createOptionSetRequest,
                string.Format("An error occured while creating the attribute: {0}",
                    attributeTemplate.LogicalName));

            createdGlobalOptionSetList.Add(attributeTemplate.GlobalOptionSetListLogicalName);
        }
        private CreateRequest GetCreateWebResourceRequest(string entityLogicalName, AttributeTemplate attributeTemplate)
        {
            var contents =
                CommonHelper.EncodeTo64(string.Format(DefaultConfiguration.WebResourceHtmlTemplate,
                    attributeTemplate.DisplayName));
            var webResource = new WebResource
            {
                Content = contents,
                DisplayName = attributeTemplate.DisplayNameShort,
                Description = attributeTemplate.Description,
                Name = entityLogicalName + "_" + attributeTemplate.LogicalName,
                LogicalName = WebResource.EntityLogicalName,
                WebResourceType = new OptionSetValue((int) Enums.WebResourceTypes.Html)
            };
            var createRequest = new CreateRequest
            {
                Target = webResource
            };

            if (!string.IsNullOrWhiteSpace(DefaultConfiguration.SolutionUniqueName))
            {
                createRequest.Parameters.Add("SolutionUniqueName", DefaultConfiguration.SolutionUniqueName);
            }

            return createRequest;
        }
 private DecimalAttributeMetadata CreateDecimalAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var decimalAttributeMetadata = new DecimalAttributeMetadata
     {
         MinValue = -100000000000.00M,
         MaxValue = 100000000000.00M,
         Precision = 2
     };
     return decimalAttributeMetadata;
 }
        private DoubleAttributeMetadata CreateFloatAttributeMetadata(AttributeTemplate attributeTemplate)
        {
            var floatAttributeMetadata = new DoubleAttributeMetadata
            {
                MinValue = -100000000000.00,
                MaxValue = 100000000000.00,
                Precision = 2
            };

            return floatAttributeMetadata;
        }
 private DateTimeAttributeMetadata CreateDateTimeAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var dateTimeAttributeMetadata = new DateTimeAttributeMetadata
     {
         Format = attributeTemplate.DateTimeFormat == default(DateTimeFormat?)
             ? DefaultConfiguration.DefaultDateTimeFormat
             : attributeTemplate.DateTimeFormat
     };
     return dateTimeAttributeMetadata;
 }
        private BooleanAttributeMetadata CreateBoolAttributeMetadata(AttributeTemplate attributeTemplate)
        {
            var yesValue = DefaultConfiguration.YesDefaultValue;
            var noValue = DefaultConfiguration.NoDefaultValue;
            if (attributeTemplate.OptionSetList != null && attributeTemplate.OptionSetList.Count == 2)
            {
                yesValue = attributeTemplate.OptionSetList[0].Label;
                noValue = attributeTemplate.OptionSetList[1].Label;
            }

            var booleanAttributeMetadata = new BooleanAttributeMetadata
            {
                OptionSet = new BooleanOptionSetMetadata(
                    new OptionMetadata(GetLabelWithLocalized(yesValue), 1),
                    new OptionMetadata(GetLabelWithLocalized(noValue), 0)
                    ),
                DefaultValue = attributeTemplate.BooleanDefaultValue == default(bool?)
                    ? DefaultConfiguration.DefaultBooleanDefaultValue
                    : attributeTemplate.BooleanDefaultValue
            };
            return booleanAttributeMetadata;
        }
        private void CreateAttribute(string entityLogicalName, AttributeTemplate attributeTemplate)
        {
            if (attributeTemplate.AttributeType == typeof (Lookup))
            {
                CreateLookupAttribute(entityLogicalName, attributeTemplate);
                return;
            }

            if (attributeTemplate.AttributeType == typeof(NNRelation))
            {
                CreateNNRelation(entityLogicalName, attributeTemplate);
                return;
            }

            if (attributeTemplate.AttributeType == typeof (GlobalOptionSet) &&
                !createdGlobalOptionSetList.Contains(attributeTemplate.GlobalOptionSetListLogicalName))
            {
                CreateGlobalOptionSetAttribute(attributeTemplate);
            }

            CreateRequest createWebRequest = null;
            if (attributeTemplate.DisplayName.Length > DefaultConfiguration.AttributeDisplayNameMaxLength)
            {
                createWebRequest = GetCreateWebResourceRequest(entityLogicalName, attributeTemplate);
            }
            var createAttributeRequest = GetCreateAttributeRequest(entityLogicalName, attributeTemplate);
            if (createAttributeRequest != null)
            {
                createAttributeRequestList.Add(createAttributeRequest);
                if (createWebRequest != null)
                {
                    createWebResourcesRequestList.Add(createWebRequest);
                }
            }
        }
 private StringAttributeMetadata CreateStringAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var stringAttributeMetadata = new StringAttributeMetadata
     {
         MaxLength = attributeTemplate.MaxLength == default(int)
             ? DefaultConfiguration.DefaultStringMaxLength
             : attributeTemplate.MaxLength,
         FormatName = attributeTemplate.StringFormatName == default(StringFormatName)
             ? DefaultConfiguration.DefaultStringFormatName
             : attributeTemplate.StringFormatName
     };
     return stringAttributeMetadata;
 }
 private PicklistAttributeMetadata CreateGlobalOptionSetAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var picklistAttributeMetadata = new PicklistAttributeMetadata
     {
         OptionSet = new OptionSetMetadata
         {
             IsGlobal = true,
             Name = attributeTemplate.GlobalOptionSetListLogicalName
         }
     };
     return picklistAttributeMetadata;
 }
        private CreateAttributeRequest GetCreateAttributeRequest(string entityLogicalName,
            AttributeTemplate attributeTemplate)
        {
            if (attributeTemplate.AttributeType == typeof(Primary))
            {
                return null;
            }

            var createAttributeRequest = new CreateAttributeRequest {EntityName = entityLogicalName};
            if (attributeTemplate.AttributeType == typeof (string))
            {
                createAttributeRequest.Attribute = CreateStringAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (int))
            {
                createAttributeRequest.Attribute = CreateIntAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (decimal))
            {
                createAttributeRequest.Attribute = CreateDecimalAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (OptionSet))
            {
                createAttributeRequest.Attribute = CreateOptionSetAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (GlobalOptionSet))
            {
                createAttributeRequest.Attribute = CreateGlobalOptionSetAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (bool))
            {
                createAttributeRequest.Attribute = CreateBoolAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (Money))
            {
                createAttributeRequest.Attribute = CreateMoneyAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (DateTime))
            {
                createAttributeRequest.Attribute = CreateDateTimeAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof (Multiline))
            {
                createAttributeRequest.Attribute = CreateMultilineAttributeMetadata(attributeTemplate);
            }
            else if (attributeTemplate.AttributeType == typeof(float))
            {
                createAttributeRequest.Attribute = CreateFloatAttributeMetadata(attributeTemplate);
            }
            else
            {
                var exception =
                    new Exception(string.Format("Given attribute type is not supported. Type: {0}",
                        attributeTemplate.AttributeType));
                errorList.Add(exception);
                return null;
            }

            createAttributeRequest.Attribute.SchemaName = attributeTemplate.LogicalName;
            createAttributeRequest.Attribute.RequiredLevel =
                new AttributeRequiredLevelManagedProperty(attributeTemplate.IsRequired
                    ? AttributeRequiredLevel.SystemRequired
                    : AttributeRequiredLevel.None);
            createAttributeRequest.Attribute.DisplayName = GetLabelWithLocalized(attributeTemplate.DisplayNameShort);
            createAttributeRequest.Attribute.Description = GetLabelWithLocalized(attributeTemplate.Description);
            if(!string.IsNullOrWhiteSpace(attributeTemplate.OtherDisplayName))
            {
                var otherDisplayLabel = new LocalizedLabel(attributeTemplate.OtherDisplayName, DefaultConfiguration.OtherLanguageCode);
                createAttributeRequest.Attribute.DisplayName.LocalizedLabels.Add(otherDisplayLabel);
            }

            if (!string.IsNullOrWhiteSpace(attributeTemplate.OtherDescription))
            {
                var otherDescriptionLabel = new LocalizedLabel(attributeTemplate.OtherDescription, DefaultConfiguration.OtherLanguageCode);
                createAttributeRequest.Attribute.Description.LocalizedLabels.Add(otherDescriptionLabel);
            }

            return createAttributeRequest;
        }
 private IntegerAttributeMetadata CreateIntAttributeMetadata(AttributeTemplate attributeTemplate)
 {
     var integerAttributeMetadata = new IntegerAttributeMetadata
     {
         MinValue = attributeTemplate.MinLength == default(int)
             ? DefaultConfiguration.DefaultIntMinValue
             : attributeTemplate.MinLength,
         MaxValue = attributeTemplate.MaxLength == default(int)
             ? DefaultConfiguration.DefaultIntMaxValue
             : attributeTemplate.MaxLength,
         Format = attributeTemplate.IntegerFormat == default(IntegerFormat?)
             ? DefaultConfiguration.DefaultIntegerFormat
             : attributeTemplate.IntegerFormat
     };
     return integerAttributeMetadata;
 }
 private OptionMetadataCollection GetOptionMetadataCollection(AttributeTemplate attributeTemplate)
 {
     var optionMetadataCollection = new OptionMetadataCollection();
     if (attributeTemplate.OptionSetList != null)
     {
         foreach (var optionMetadataTemplate in attributeTemplate.OptionSetList)
         {
             var optionMetadata = new OptionMetadata
             {
                 Description = GetLabelWithLocalized(optionMetadataTemplate.Description),
                 Label = GetLabelWithLocalized(optionMetadataTemplate.Label),
                 Value = optionMetadataTemplate.Value
             };
             optionMetadataCollection.Add(optionMetadata);
         }
     }
     return optionMetadataCollection;
 }
        private bool ApplyFileSpecificOperations(string excelFile, List<Exception> errorList,
            AttributeTemplate attributeTemplate,
            Range xlsRange, int currentRow)
        {
            if (attributeTemplate.AttributeType == typeof (Lookup))
            {
                attributeTemplate.LookupEntityLogicalName = GetCellValueAsString(xlsRange, currentRow,
                    attributeLookupEntityLogicalNameColumn);
                if (string.IsNullOrWhiteSpace(attributeTemplate.LookupEntityLogicalName))
                {
                    errorList.Add(
                        new Exception(
                            string.Format(
                                "Attribute LookupEntityLogicalName can not be empty for LookUp fields. File: {0}",
                                excelFile)));
                    return true;
                }

                attributeTemplate.LookupEntityLogicalName = attributeTemplate.LookupEntityLogicalName.Trim();
            }
            else if (attributeTemplate.AttributeType == typeof (OptionSet))
            {
                attributeTemplate.OptionSetList =
                    GetOptionSetList(GetCellValueAsString(xlsRange, currentRow, attributeOptionSetListColumn));
            }
            else if (attributeTemplate.AttributeType == typeof (bool))
            {
                attributeTemplate.OptionSetList =
                    GetOptionSetList(GetCellValueAsString(xlsRange, currentRow, attributeOptionSetListColumn));
            }
            else if (attributeTemplate.AttributeType == typeof (GlobalOptionSet))
            {
                attributeTemplate.OptionSetList =
                    GetOptionSetList(GetCellValueAsString(xlsRange, currentRow, attributeOptionSetListColumn));
                attributeTemplate.GlobalOptionSetListLogicalName = GetCellValueAsString(xlsRange, currentRow,
                    attributeGlobalOptionSetListLogicalNameColumn);
                if (string.IsNullOrWhiteSpace(attributeTemplate.GlobalOptionSetListLogicalName))
                {
                    errorList.Add(
                        new Exception(
                            string.Format(
                                "Attribute GlobalOptionSetListLogicalName can not be empty for GlobalOptionSet fields. File: {0}",
                                excelFile)));
                    return true;
                }
                attributeTemplate.GlobalOptionSetListLogicalName =
                    attributeTemplate.GlobalOptionSetListLogicalName.Trim();
            }
            else if (attributeTemplate.AttributeType == typeof(NNRelation))
            {
                attributeTemplate.LookupEntityLogicalName = GetCellValueAsString(xlsRange, currentRow,
                    attributeLookupEntityLogicalNameColumn);
                if (string.IsNullOrWhiteSpace(attributeTemplate.LookupEntityLogicalName))
                {
                    errorList.Add(
                        new Exception(
                            string.Format(
                                "Attribute LookupEntityLogicalName can not be empty for NN fields. File: {0}",
                                excelFile)));
                    return true;
                }
            }
            return false;
        }