public EntityTemplate GetEntityTemplateFromFile(string excelFile) { var entityTemplate = new EntityTemplate {AttributeList = new List<AttributeTemplate>(),Warnings = new List<Exception>(),Errors = new List<Exception>()}; var xlsWorksheet = (Worksheet) xlsWorkbook.Worksheets.Item[defaultEntitySheetIndex]; var xlsRange = xlsWorksheet.UsedRange; if (GetEntityTemplate(excelFile, entityTemplate.Errors, entityTemplate, xlsRange)) { entityTemplate.WillCreateEntity = false; } else { entityTemplate.WillCreateEntity = true; } for (var currentRow = attributeTemplateFirstRow; currentRow <= xlsRange.Rows.Count; currentRow++) { var logicalName = GetCellValueAsString(xlsRange, currentRow, attributeLogicalNameColumn); var displayName = GetCellValueAsString(xlsRange, currentRow, attributeDisplayNameColumn); if (string.IsNullOrWhiteSpace(logicalName) || string.IsNullOrWhiteSpace(displayName)) { //entityTemplate.Errors.Add( // new Exception(string.Format("Attribute LogicalName or DisplayName can not be empty. File: {0}", // excelFile))); continue; } var attributeTemplate = GetAttributeTemplate(xlsRange, currentRow, logicalName, excelFile, entityTemplate.Errors, entityTemplate.Warnings); if (attributeTemplate == null) { continue; } if (ApplyFileSpecificOperations(excelFile, entityTemplate.Errors, attributeTemplate, xlsRange, currentRow)) { continue; } entityTemplate.AttributeList.Add(attributeTemplate); } if (xlsWorkbook.Worksheets.Count > 1) { var webresourceSheet = (Worksheet)xlsWorkbook.Worksheets.Item[defaultWebresourceSheetIndex]; CreateWebResources(webresourceSheet, entityTemplate, excelFile, entityTemplate.Errors, entityTemplate.Warnings); } return entityTemplate; }
private StringAttributeMetadata GetPrimaryAttribute(EntityTemplate entityTemplate) { var primaryAttribute = (from attribute in entityTemplate.AttributeList where attribute.AttributeType == typeof(Primary) select attribute).FirstOrDefault(); var stringAttributeMetadata = new StringAttributeMetadata { SchemaName = primaryAttribute != null ? primaryAttribute.LogicalName : DefaultConfiguration.DefaultPrimaryAttribute, RequiredLevel = new AttributeRequiredLevelManagedProperty( primaryAttribute != null && primaryAttribute.IsRequired ? AttributeRequiredLevel.SystemRequired : AttributeRequiredLevel.None), DisplayName = GetLabelWithLocalized(primaryAttribute != null ? primaryAttribute.DisplayNameShort : DefaultConfiguration.DefaultPrimaryAttributeDisplayName), Description = GetLabelWithLocalized(primaryAttribute != null ? primaryAttribute.Description : DefaultConfiguration.DefaultPrimaryAttributeDescription), MaxLength = primaryAttribute != null ? primaryAttribute.MaxLength : DefaultConfiguration.DefaultStringMaxLength, FormatName = DefaultConfiguration.DefaultStringFormatName }; return stringAttributeMetadata; }
public void CreateEntity(string excelFile, EntityTemplate entityTemplate, Action<string> setMessageOfTheDayLabel) { errorList = new List<Exception>(); warningList = new List<Exception>(); createAttributeRequestList = new List<CreateAttributeRequest>(); createdGlobalOptionSetList = new List<string>(); createWebResourcesRequestList = new List<CreateRequest>(); if (entityTemplate == null) { errorList.Add( new Exception(string.Format("CreateEntity method EntityTemplate reference is null. File: {0}", excelFile))); entityTemplate=new EntityTemplate(){Errors = new List<Exception>()}; entityTemplate.Errors.AddRange(errorList); return; } if (entityTemplate.WillCreateEntity) { var createrequest = new CreateEntityRequest(); var entityMetadata = new EntityMetadata { SchemaName = entityTemplate.LogicalName, DisplayName = GetLabelWithLocalized(entityTemplate.DisplayName), DisplayCollectionName = GetLabelWithLocalized(entityTemplate.DisplayNamePlural), Description = GetLabelWithLocalized(entityTemplate.Description), OwnershipType = DefaultConfiguration.DefaultOwnershipType, IsActivity = false }; createrequest.PrimaryAttribute = GetPrimaryAttribute(entityTemplate); createrequest.Entity = entityMetadata; ExecuteOperation(GetSharedOrganizationService(), createrequest, string.Format("An error occured while creating the entity: {0}", entityTemplate.LogicalName)); //setMessageOfTheDayLabel("1"); if (entityTemplate.AttributeList == null) { entityTemplate.Warnings.AddRange(warningList); entityTemplate.Errors.AddRange(errorList); } foreach (var attributeTemplate in entityTemplate.AttributeList) { CreateAttribute(entityTemplate.LogicalName, attributeTemplate); } } var requests = new ExecuteMultipleRequest { Requests = new OrganizationRequestCollection() }; if (entityTemplate.WebResource != null) { for (var i = 0; i < entityTemplate.WebResource.Count; i++) { var webresourceRequest = GetCreateWebResourceRequest(entityTemplate.WebResource[i]); requests.Requests.Add(webresourceRequest); if (i % DefaultConfiguration.ExecuteMultipleSize == 0 || createAttributeRequestList.Count - i < 10) { ExecuteMultipleWebresourceRequests(requests); //setMessageOfTheDayLabel(requests.Requests.Count); requests = new ExecuteMultipleRequest { Requests = new OrganizationRequestCollection() }; } } } ExecuteMultipleOperation("Execute multiple error occured.", setMessageOfTheDayLabel); entityTemplate.Warnings.AddRange(warningList); entityTemplate.Errors.AddRange(errorList); }
private void CreateWebResources(Worksheet webresourceSheet, EntityTemplate entityTemplate, string excelFile, List<Exception> error, List<Exception> warning) { var xlsRange = webresourceSheet.UsedRange; for (var currentRow = webresourceTemplateFirstRow; currentRow < xlsRange.Rows.Count; currentRow++) { var logicalName = GetCellValueAsString(xlsRange, currentRow, webresourceLogicalNameColumn).Replace("\n", ""); var displayName = GetCellValueAsString(xlsRange, currentRow, webresourceDisplayNameColumn).Replace("\n", ""); var description = GetCellValueAsString(xlsRange, currentRow, webresourceDescriptionColumn).Replace("\n", ""); var type = GetCellValueAsString(xlsRange, currentRow, webresourceTypeColumn).Replace("\n", ""); var content = CommonHelper.EncodeTo64(GetCellValueAsString(xlsRange, currentRow, webresourceContentColumn)); var resourceType = CommonHelper.GetWebResourceType(type, excelFile, error, warning); if (resourceType < 0) { continue; } var webResource = new WebResource { Content = content, DisplayName = displayName, Description = description, Name = logicalName, LogicalName = WebResource.EntityLogicalName, WebResourceType = new OptionSetValue(resourceType) }; entityTemplate.WebResource = entityTemplate.WebResource ?? new List<WebResource>(); entityTemplate.WebResource.Add(webResource); } }
private static bool GetEntityTemplate(string excelFile, List<Exception> error, EntityTemplate entityTemplate, Range xlsRange) { entityTemplate.LogicalName = GetCellValueAsString(xlsRange, entityTemplateDefinationRow, entityLogicalNameColumn); entityTemplate.DisplayName = GetCellValueAsString(xlsRange, entityTemplateDefinationRow, entityDisplayNameColumn); entityTemplate.Description = GetCellValueAsString(xlsRange, entityTemplateDefinationRow, entityDescriptionColumn); entityTemplate.DisplayNamePlural = GetCellValueAsString(xlsRange, entityTemplateDefinationRow, entityDisplayNamePluralColumn); if (string.IsNullOrWhiteSpace(entityTemplate.LogicalName) || string.IsNullOrWhiteSpace(entityTemplate.DisplayName) || string.IsNullOrWhiteSpace(entityTemplate.DisplayNamePlural)) { error.Add( new Exception( string.Format( "Entity LogicalName, DisplayName or DisplayNamePlural can not be empty. File: {0}", excelFile))); return true; } entityTemplate.LogicalName = entityTemplate.LogicalName.ToLower().Trim(); entityTemplate.DisplayName = entityTemplate.DisplayName.Trim(); entityTemplate.DisplayNamePlural = entityTemplate.DisplayName.Trim(); return false; }