public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorkbook file, IOrganizationService service, FormExportOption options)
public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorkbook file, IOrganizationService service, FormExportOption options) { // Retrieve current user language information var setting = GetCurrentUserSettings(service); var userSettingLcid = setting.GetAttributeValue <int>("uilanguageid"); var currentSetting = userSettingLcid; var crmForms = new List <CrmForm>(); var crmFormTabs = new List <CrmFormTab>(); var crmFormSections = new List <CrmFormSection>(); var crmFormLabels = new List <CrmFormLabel>(); foreach (var lcid in languages) { if (userSettingLcid != lcid) { setting["localeid"] = lcid; setting["uilanguageid"] = lcid; setting["helplanguageid"] = lcid; service.Update(setting); currentSetting = lcid; } foreach (var entity in entities.OrderBy(e => e.LogicalName)) { if (!entity.MetadataId.HasValue) { continue; } var forms = RetrieveEntityFormList(entity.LogicalName, service); foreach (var form in forms) { #region Tabs if (options.ExportFormTabs || options.ExportFormSections || options.ExportFormFields) { // Load Xml definition of form var sFormXml = form.GetAttributeValue <string>("formxml"); var formXml = new XmlDocument(); formXml.LoadXml(sFormXml); foreach (XmlNode tabNode in formXml.SelectNodes("//tab")) { var tabName = ExtractTabName(tabNode, lcid, crmFormTabs, form, entity); #region Sections if (options.ExportFormSections || options.ExportFormFields) { foreach ( XmlNode sectionNode in tabNode.SelectNodes("columns/column/sections/section")) { var sectionName = ExtractSection(sectionNode, lcid, crmFormSections, form, tabName, entity); #region Labels if (options.ExportFormFields) { foreach (XmlNode labelNode in sectionNode.SelectNodes("rows/row/cell")) { ExtractField(labelNode, crmFormLabels, form, tabName, sectionName, entity, lcid); } } #endregion Labels } } #endregion Sections } } #endregion Tabs } } } if (userSettingLcid != currentSetting) { setting["localeid"] = userSettingLcid; setting["uilanguageid"] = userSettingLcid; setting["helplanguageid"] = userSettingLcid; service.Update(setting); } foreach (var entity in entities.OrderBy(e => e.LogicalName)) { if (!entity.MetadataId.HasValue) { continue; } var forms = RetrieveEntityFormList(entity.LogicalName, service); foreach (var form in forms) { var crmForm = crmForms.FirstOrDefault(f => f.FormUniqueId == form.GetAttributeValue <Guid>("formidunique")); if (crmForm == null) { crmForm = new CrmForm { FormUniqueId = form.GetAttributeValue <Guid>("formidunique"), Id = form.GetAttributeValue <Guid>("formid"), Entity = entity.LogicalName, Names = new Dictionary <int, string>(), Descriptions = new Dictionary <int, string>() }; crmForms.Add(crmForm); } // Names var request = new RetrieveLocLabelsRequest { AttributeName = "name", EntityMoniker = new EntityReference("systemform", form.Id) }; var response = (RetrieveLocLabelsResponse)service.Execute(request); foreach (var locLabel in response.Label.LocalizedLabels) { crmForm.Names.Add(locLabel.LanguageCode, locLabel.Label); } // Descriptions request = new RetrieveLocLabelsRequest { AttributeName = "description", EntityMoniker = new EntityReference("systemform", form.Id) }; response = (RetrieveLocLabelsResponse)service.Execute(request); foreach (var locLabel in response.Label.LocalizedLabels) { crmForm.Descriptions.Add(locLabel.LanguageCode, locLabel.Label); } } } var line = 1; if (options.ExportForms) { var formSheet = file.Worksheets.Add("Forms"); AddFormHeader(formSheet, languages); foreach (var crmForm in crmForms) { line = ExportForm(languages, formSheet, line, crmForm); } // Applying style to cells for (int i = 0; i < (4 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(formSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 4; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(formSheet, i, j).Style); } } } if (options.ExportFormTabs) { var tabSheet = file.Worksheets.Add("Forms Tabs"); line = 1; AddFormTabHeader(tabSheet, languages); foreach (var crmFormTab in crmFormTabs) { line = ExportTab(languages, tabSheet, line, crmFormTab); } // Applying style to cells for (int i = 0; i < (5 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(tabSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 5; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(tabSheet, i, j).Style); } } } if (options.ExportFormSections) { var sectionSheet = file.Worksheets.Add("Forms Sections"); line = 1; AddFormSectionHeader(sectionSheet, languages); foreach (var crmFormSection in crmFormSections) { line = ExportSection(languages, sectionSheet, line, crmFormSection); } // Applying style to cells for (int i = 0; i < (6 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(sectionSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 6; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sectionSheet, i, j).Style); } } } if (options.ExportFormFields) { var labelSheet = file.Worksheets.Add("Forms Fields"); AddFormLabelsHeader(labelSheet, languages); line = 1; foreach (var crmFormLabel in crmFormLabels) { line = ExportField(languages, labelSheet, line, crmFormLabel); } // Applying style to cells for (int i = 0; i < (8 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(labelSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 8; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(labelSheet, i, j).Style); } } } }
public void Export(List<EntityMetadata> entities, List<int> languages, ExcelWorkbook file, IOrganizationService service, FormExportOption options) { // Retrieve current user language information var setting = GetCurrentUserSettings(service); var userSettingLcid = setting.GetAttributeValue<int>("uilanguageid"); var currentSetting = userSettingLcid; var crmForms = new List<CrmForm>(); var crmFormTabs = new List<CrmFormTab>(); var crmFormSections = new List<CrmFormSection>(); var crmFormLabels = new List<CrmFormLabel>(); foreach (var lcid in languages) { if (userSettingLcid != lcid) { setting["localeid"] = lcid; setting["uilanguageid"] = lcid; setting["helplanguageid"] = lcid; service.Update(setting); currentSetting = lcid; } foreach (var entity in entities.OrderBy(e => e.LogicalName)) { if (!entity.MetadataId.HasValue) continue; var forms = RetrieveEntityFormList(entity.LogicalName, service); foreach (var form in forms) { #region Tabs if (options.ExportFormTabs || options.ExportFormSections || options.ExportFormFields) { // Load Xml definition of form var sFormXml = form.GetAttributeValue<string>("formxml"); var formXml = new XmlDocument(); formXml.LoadXml(sFormXml); foreach (XmlNode tabNode in formXml.SelectNodes("//tab")) { var tabName = ExtractTabName(tabNode, lcid, crmFormTabs, form, entity); #region Sections if (options.ExportFormSections || options.ExportFormFields) { foreach ( XmlNode sectionNode in tabNode.SelectNodes("columns/column/sections/section")) { var sectionName = ExtractSection(sectionNode, lcid, crmFormSections, form, tabName, entity); #region Labels if (options.ExportFormFields) { foreach (XmlNode labelNode in sectionNode.SelectNodes("rows/row/cell")) { ExtractField(labelNode, crmFormLabels, form, tabName, sectionName, entity, lcid); } } #endregion Labels } } #endregion Sections } } #endregion Tabs } } } if (userSettingLcid != currentSetting) { setting["localeid"] = userSettingLcid; setting["uilanguageid"] = userSettingLcid; setting["helplanguageid"] = userSettingLcid; service.Update(setting); } foreach (var entity in entities.OrderBy(e => e.LogicalName)) { if (!entity.MetadataId.HasValue) continue; var forms = RetrieveEntityFormList(entity.LogicalName, service); foreach (var form in forms) { var crmForm = crmForms.FirstOrDefault(f => f.FormUniqueId == form.GetAttributeValue<Guid>("formidunique")); if (crmForm == null) { crmForm = new CrmForm { FormUniqueId = form.GetAttributeValue<Guid>("formidunique"), Id = form.GetAttributeValue<Guid>("formid"), Entity = entity.LogicalName, Names = new Dictionary<int, string>(), Descriptions = new Dictionary<int, string>() }; crmForms.Add(crmForm); } // Names var request = new RetrieveLocLabelsRequest { AttributeName = "name", EntityMoniker = new EntityReference("systemform", form.Id) }; var response = (RetrieveLocLabelsResponse)service.Execute(request); foreach (var locLabel in response.Label.LocalizedLabels) { crmForm.Names.Add(locLabel.LanguageCode, locLabel.Label); } // Descriptions request = new RetrieveLocLabelsRequest { AttributeName = "description", EntityMoniker = new EntityReference("systemform", form.Id) }; response = (RetrieveLocLabelsResponse)service.Execute(request); foreach (var locLabel in response.Label.LocalizedLabels) { crmForm.Descriptions.Add(locLabel.LanguageCode, locLabel.Label); } } } var line = 1; if (options.ExportForms) { var formSheet = file.Worksheets.Add("Forms"); AddFormHeader(formSheet, languages); foreach (var crmForm in crmForms) { line = ExportForm(languages, formSheet, line, crmForm); } // Applying style to cells for (int i = 0; i < (4 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(formSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 4; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(formSheet, i, j).Style); } } } if (options.ExportFormTabs) { var tabSheet = file.Worksheets.Add("Forms Tabs"); line = 1; AddFormTabHeader(tabSheet, languages); foreach (var crmFormTab in crmFormTabs) { line = ExportTab(languages, tabSheet, line, crmFormTab); } // Applying style to cells for (int i = 0; i < (5 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(tabSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 5; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(tabSheet, i, j).Style); } } } if (options.ExportFormSections) { var sectionSheet = file.Worksheets.Add("Forms Sections"); line = 1; AddFormSectionHeader(sectionSheet, languages); foreach (var crmFormSection in crmFormSections) { line = ExportSection(languages, sectionSheet, line, crmFormSection); } // Applying style to cells for (int i = 0; i < (6 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(sectionSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 6; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sectionSheet, i, j).Style); } } } if (options.ExportFormFields) { var labelSheet = file.Worksheets.Add("Forms Fields"); AddFormLabelsHeader(labelSheet, languages); line = 1; foreach (var crmFormLabel in crmFormLabels) { line = ExportField(languages, labelSheet, line, crmFormLabel); } // Applying style to cells for (int i = 0; i < (8 + languages.Count); i++) { StyleMutator.TitleCell(ZeroBasedSheet.Cell(labelSheet, 0, i).Style); } for (int i = 1; i < line; i++) { for (int j = 0; j < 8; j++) { StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(labelSheet, i, j).Style); } } } }