示例#1
0
        public void Export(List <int> languages, ExcelWorksheet sheet, bool onlyUnmanaged, IOrganizationService service)
        {
            var qe = new QueryByAttribute("ribbondiff")
            {
                ColumnSet = new ColumnSet(true)
            };

            if (onlyUnmanaged)
            {
                qe.Attributes.AddRange("difftype", "ismanaged");
                qe.Values.AddRange(3, false);
            }
            else
            {
                qe.Attributes.AddRange("difftype");
                qe.Values.AddRange(3);
            }
            qe.AddOrder("entity", OrderType.Ascending);

            var records = service.RetrieveMultiple(qe);

            var line = 1;

            AddHeader(sheet, languages);

            foreach (var record in records.Entities)
            {
                var cell = 0;

                ZeroBasedSheet.Cell(sheet, line, cell++).Value = record.Id.ToString("B");
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = record.GetAttributeValue <string>("entity");
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = record.GetAttributeValue <string>("diffid");

                var xml = new XmlDocument();
                xml.LoadXml(record.GetAttributeValue <string>("rdx"));

                foreach (var lcid in languages)
                {
                    var labelNode = xml.SelectSingleNode(string.Format("LocLabel/Titles/Title[@languagecode='{0}']", lcid));
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = labelNode == null ? string.Empty : labelNode.Attributes["description"].Value;
                }

                line++;
            }

            // Applying style to cells
            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        internal void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet)
        {
            var line = 1;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                foreach (var rel in entity.ManyToManyRelationships.ToList())
                {
                    var cell = 0;

                    var amc = rel.Entity1LogicalName == entity.LogicalName ? rel.Entity1AssociatedMenuConfiguration : rel.Entity2AssociatedMenuConfiguration;

                    if (!(amc.Behavior.HasValue && amc.Behavior.Value == AssociatedMenuBehavior.UseLabel))
                    {
                        continue;
                    }

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.IntersectEntityName;

                    foreach (var lcid in languages)
                    {
                        var entity1Label = string.Empty;

                        if (amc.Label != null)
                        {
                            var displayNameLabel =
                                amc.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (displayNameLabel != null)
                            {
                                entity1Label = displayNameLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity1Label;
                    }

                    line++;
                }
            }

            // Applying style to cells
            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
示例#3
0
        internal void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet)
        {
            var line = 1;

            AddHeader(sheet, languages);
            var exportedRelationships = new List <Guid>();

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                var relationships = new List <OneToManyRelationshipMetadata>();
                relationships.AddRange(entity.OneToManyRelationships);
                relationships.AddRange(entity.ManyToOneRelationships);

                foreach (var rel in relationships)
                {
                    if (exportedRelationships.Contains(rel.MetadataId.Value))
                    {
                        continue;
                    }
                    exportedRelationships.Add(rel.MetadataId.Value);

                    var cell = 0;

                    if (!rel.AssociatedMenuConfiguration.Behavior.HasValue ||
                        rel.AssociatedMenuConfiguration.Behavior.Value != AssociatedMenuBehavior.UseLabel)
                    {
                        continue;
                    }

                    // entity1Label
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.ReferencedEntity;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.SchemaName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = rel.ReferencingEntity;

                    foreach (var lcid in languages)
                    {
                        var entity1Label = string.Empty;

                        if (rel.AssociatedMenuConfiguration.Label != null)
                        {
                            var displayNameLabel =
                                rel.AssociatedMenuConfiguration.Label.LocalizedLabels.FirstOrDefault(
                                    l => l.LanguageCode == lcid);
                            if (displayNameLabel != null)
                            {
                                entity1Label = displayNameLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity1Label;
                    }

                    line++;
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        public void Export(List <int> languages, ExcelWorkbook file, IOrganizationService service)
        {
            // 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;
                }

                var forms = RetrieveDashboardList(service);

                foreach (var form in forms)
                {
                    #region Tabs

                    // 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);

                        #region Sections

                        foreach (
                            XmlNode sectionNode in tabNode.SelectNodes("columns/column/sections/section"))
                        {
                            var sectionName = ExtractSection(sectionNode, lcid, crmFormSections, form, tabName);

                            #region Labels

                            foreach (XmlNode labelNode in sectionNode.SelectNodes("rows/row/cell"))
                            {
                                ExtractField(labelNode, crmFormLabels, form, tabName, sectionName,
                                             lcid);
                            }

                            #endregion Labels
                        }

                        #endregion Sections
                    }

                    #endregion Tabs
                }
            }

            if (userSettingLcid != currentSetting)
            {
                setting["localeid"]       = userSettingLcid;
                setting["uilanguageid"]   = userSettingLcid;
                setting["helplanguageid"] = userSettingLcid;
                service.Update(setting);
            }

            var forms2 = RetrieveDashboardList(service);

            foreach (var form in forms2)
            {
                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"),
                        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;
            var formSheet = file.Worksheets.Add("Dashboards");
            AddFormHeader(formSheet, languages);

            foreach (var crmForm in crmForms)
            {
                line = ExportForm(languages, formSheet, line, crmForm);
            }

            // Applying style to cells
            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(formSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(formSheet, i, j).Style);
                }
            }

            var tabSheet = file.Worksheets.Add("Dashboards 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 < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(tabSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(tabSheet, i, j).Style);
                }
            }

            var sectionSheet = file.Worksheets.Add("Dashboards 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 < (5 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sectionSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sectionSheet, i, j).Style);
                }
            }

            var labelSheet = file.Worksheets.Add("Dashboards 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 < (7 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(labelSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(labelSheet, i, j).Style);
                }
            }
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// attributeId;entityLogicalName;attributeLogicalName;OptionSetValue;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        /// <param name="settings"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, ExportSettings settings)
        {
            var line = 0;
            int cell;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                foreach (var attribute in entity.Attributes.OrderBy(a => a.LogicalName))
                {
                    if (attribute.AttributeType == null ||
                        attribute.AttributeType.Value != AttributeTypeCode.Boolean ||
                        !attribute.MetadataId.HasValue)
                    {
                        continue;
                    }

                    var bAmd = (BooleanAttributeMetadata)attribute;

                    if (bAmd.OptionSet?.IsGlobal ?? false)
                    {
                        continue;
                    }

                    if (settings.ExportNames)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bAmd.OptionSet.FalseOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bAmd.OptionSet.FalseOption.Label != null)
                            {
                                var optionLabel =
                                    bAmd.OptionSet.FalseOption.Label.LocalizedLabels.FirstOrDefault(l =>
                                                                                                    l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bAmd.OptionSet.FalseOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bAmd.OptionSet.FalseOption.Description != null)
                            {
                                var optionLabel =
                                    bAmd.OptionSet.FalseOption.Description.LocalizedLabels.FirstOrDefault(l =>
                                                                                                          l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportNames)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bAmd.OptionSet.TrueOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bAmd.OptionSet.TrueOption.Label != null)
                            {
                                var optionLabel =
                                    bAmd.OptionSet.TrueOption.Label.LocalizedLabels.FirstOrDefault(l =>
                                                                                                   l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bAmd.OptionSet.TrueOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bAmd.OptionSet.TrueOption.Description != null)
                            {
                                var optionLabel =
                                    bAmd.OptionSet.TrueOption.Description.LocalizedLabels.FirstOrDefault(l =>
                                                                                                         l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (5 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// attributeId;entityLogicalName;attributeLogicalName;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet)
        {
            var line = 1;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                foreach (var attribute in entity.Attributes.OrderBy(a => a.LogicalName))
                {
                    var cell = 0;

                    if (attribute.AttributeType == null ||
                        attribute.AttributeType.Value == AttributeTypeCode.BigInt ||
                        attribute.AttributeType.Value == AttributeTypeCode.CalendarRules ||
                        attribute.AttributeType.Value == AttributeTypeCode.EntityName ||
                        attribute.AttributeType.Value == AttributeTypeCode.ManagedProperty ||
                        attribute.AttributeType.Value == AttributeTypeCode.Uniqueidentifier ||
                        attribute.AttributeType.Value == AttributeTypeCode.Virtual ||
                        attribute.AttributeOf != null ||
                        !attribute.MetadataId.HasValue ||
                        !attribute.IsRenameable.Value)
                    {
                        continue;
                    }

                    if (attribute.DisplayName != null && attribute.DisplayName.LocalizedLabels.All(l => string.IsNullOrEmpty(l.Label)))
                    {
                        continue;
                    }

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;

                    // DisplayName
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "DisplayName";

                    foreach (var lcid in languages)
                    {
                        var displayName = string.Empty;

                        if (attribute.DisplayName != null)
                        {
                            var displayNameLabel = attribute.DisplayName.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (displayNameLabel != null)
                            {
                                displayName = displayNameLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = displayName;
                    }

                    // Description
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        var description = string.Empty;

                        if (attribute.Description != null)
                        {
                            var descriptionLabel = attribute.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (descriptionLabel != null)
                            {
                                description = descriptionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = description;
                    }

                    line++;
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// viewId;entityLogicalName;viewName;ViewType;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="languages"></param>
        /// <param name="file"></param>
        /// <param name="service"></param>
        public void Export(List <int> languages, ExcelWorkbook file, IOrganizationService service)
        {
            var line = 1;

            var siteMap    = GetSiteMap(service);
            var siteMapDoc = new XmlDocument();

            siteMapDoc.LoadXml(siteMap["sitemapxml"].ToString());

            var crmSiteMapAreas    = new List <CrmSiteMapArea>();
            var crmSiteMapGroups   = new List <CrmSiteMapGroup>();
            var crmSiteMapSubAreas = new List <CrmSiteMapSubArea>();

            #region Export Area

            var areaNodes = siteMapDoc.SelectNodes("SiteMap/Area");
            foreach (XmlNode areaNode in areaNodes)
            {
                var area = new CrmSiteMapArea {
                    Id = areaNode.Attributes["Id"].Value
                };
                foreach (XmlNode titleNode in areaNode.SelectNodes("Titles/Title"))
                {
                    area.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Title"].Value);
                }
                foreach (XmlNode titleNode in areaNode.SelectNodes("Descriptions/Description"))
                {
                    area.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Description"].Value);
                }

                crmSiteMapAreas.Add(area);

                #region Export Groups

                var groupNodes = areaNode.SelectNodes("Group");
                foreach (XmlNode groupNode in groupNodes)
                {
                    var group = new CrmSiteMapGroup
                    {
                        Id     = groupNode.Attributes["Id"].Value,
                        AreaId = areaNode.Attributes["Id"].Value
                    };
                    foreach (XmlNode titleNode in groupNode.SelectNodes("Titles/Title"))
                    {
                        group.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Title"].Value);
                    }
                    foreach (XmlNode titleNode in groupNode.SelectNodes("Descriptions/Description"))
                    {
                        group.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Description"].Value);
                    }

                    crmSiteMapGroups.Add(group);

                    #region Export SubArea

                    var subAreaNodes = groupNode.SelectNodes("SubArea");
                    foreach (XmlNode subAreaNode in subAreaNodes)
                    {
                        var subArea = new CrmSiteMapSubArea()
                        {
                            Id      = subAreaNode.Attributes["Id"].Value,
                            GroupId = groupNode.Attributes["Id"].Value,
                            AreaId  = areaNode.Attributes["Id"].Value
                        };
                        foreach (XmlNode titleNode in subAreaNode.SelectNodes("Titles/Title"))
                        {
                            subArea.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Title"].Value);
                        }
                        foreach (XmlNode titleNode in subAreaNode.SelectNodes("Descriptions/Description"))
                        {
                            subArea.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value), titleNode.Attributes["Description"].Value);
                        }

                        crmSiteMapSubAreas.Add(subArea);
                    }

                    #endregion Export SubArea
                }

                #endregion Export Groups
            }

            #endregion Export Area

            #region Area sheet

            var areaSheet = file.Worksheets.Add("SiteMap Areas");
            AddAreaHeader(areaSheet, languages);
            foreach (var crmArea in crmSiteMapAreas)
            {
                var cell = 0;
                ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Id;
                ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = "Title";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                }

                line++;
                cell = 0;
                ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Id;
                ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = "Description";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                }
                line++;
            }

            #endregion Area sheet

            #region Group sheet

            line = 1;
            var groupSheet = file.Worksheets.Add("SiteMap Groups");
            AddGroupHeader(groupSheet, languages);
            foreach (var crmGroup in crmSiteMapGroups)
            {
                var cell = 0;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.AreaId;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Id;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = "Title";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                }

                line++;
                cell = 0;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.AreaId;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Id;
                ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = "Description";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                }
                line++;
            }

            #endregion Group sheet

            #region SubArea sheet

            line = 1;
            var subAreaSheet = file.Worksheets.Add("SiteMap SubAreas");
            AddSubAreaHeader(subAreaSheet, languages);
            foreach (var crmSubArea in crmSiteMapSubAreas)
            {
                var cell = 0;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.AreaId;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.GroupId;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Id;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = "Title";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                }

                line++;
                cell = 0;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.AreaId;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.GroupId;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Id;
                ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = "Description";

                foreach (var lcid in languages)
                {
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                }
                line++;
            }

            #endregion SubArea sheet

            // Applying style to cells
            for (int i = 0; i < (2 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(areaSheet, 0, i).Style);
            }
            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(areaSheet, i, j).Style);
                }
            }

            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(groupSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(groupSheet, i, j).Style);
                }
            }

            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(subAreaSheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(subAreaSheet, i, j).Style);
                }
            }
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// viewId;entityLogicalName;viewName;ViewType;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, IOrganizationService service, ExportSettings settings)
        {
            var line = 0;
            var cell = 0;

            AddHeader(sheet, languages);

            var crmViews = new List <CrmView>();

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                if (!entity.MetadataId.HasValue)
                {
                    continue;
                }

                var views = RetrieveViews(entity.LogicalName, entity.ObjectTypeCode.Value, service);

                foreach (var view in views)
                {
                    var crmView = crmViews.FirstOrDefault(cv => cv.Id == view.Id);
                    if (crmView == null)
                    {
                        crmView = new CrmView
                        {
                            Id           = view.Id,
                            Entity       = view.GetAttributeValue <string>("returnedtypecode"),
                            Type         = view.GetAttributeValue <int>("querytype"),
                            Names        = new Dictionary <int, string>(),
                            Descriptions = new Dictionary <int, string>()
                        };
                        crmViews.Add(crmView);
                    }

                    RetrieveLocLabelsRequest  request;
                    RetrieveLocLabelsResponse response;

                    if (settings.ExportNames)
                    {
                        // Names
                        request = new RetrieveLocLabelsRequest
                        {
                            AttributeName = "name",
                            EntityMoniker = new EntityReference("savedquery", view.Id)
                        };

                        response = (RetrieveLocLabelsResponse)service.Execute(request);
                        foreach (var locLabel in response.Label.LocalizedLabels)
                        {
                            crmView.Names.Add(locLabel.LanguageCode, locLabel.Label);
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        // Descriptions
                        request = new RetrieveLocLabelsRequest
                        {
                            AttributeName = "description",
                            EntityMoniker = new EntityReference("savedquery", view.Id)
                        };

                        response = (RetrieveLocLabelsResponse)service.Execute(request);
                        foreach (var locLabel in response.Label.LocalizedLabels)
                        {
                            crmView.Descriptions.Add(locLabel.LanguageCode, locLabel.Label);
                        }
                    }
                }
            }

            foreach (var crmView in crmViews.OrderBy(cv => cv.Entity).ThenBy(cv => cv.Type))
            {
                if (settings.ExportNames)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmView.Id.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmView.Entity;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = _viewTypes.ContainsKey(crmView.Type) ?_viewTypes[crmView.Type] : crmView.Type.ToString();
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Name";

                    foreach (var lcid in languages)
                    {
                        var name = crmView.Names.FirstOrDefault(n => n.Key == lcid);
                        if (name.Value != null)
                        {
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = name.Value;
                        }
                        else
                        {
                            cell++;
                        }
                    }
                }

                if (settings.ExportDescriptions)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmView.Id.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmView.Entity;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = _viewTypes.ContainsKey(crmView.Type) ? _viewTypes[crmView.Type] : crmView.Type.ToString();
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        var desc = crmView.Descriptions.FirstOrDefault(n => n.Key == lcid);
                        if (desc.Value != null)
                        {
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = desc.Value;
                        }
                        else
                        {
                            cell++;
                        }
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// entityId;entityLogicalName;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        /// <param name="settings"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, ExportSettings settings)
        {
            var line = 0;
            int cell;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                if (!entity.MetadataId.HasValue)
                {
                    continue;
                }

                if (settings.ExportNames)
                {
                    line++;
                    cell = 0;

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;

                    // DisplayName
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "DisplayName";

                    foreach (var lcid in languages)
                    {
                        var displayName = string.Empty;

                        if (entity.DisplayName != null)
                        {
                            var displayNameLabel =
                                entity.DisplayName.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (displayNameLabel != null)
                            {
                                displayName = displayNameLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = displayName;
                    }

                    // Plural Name
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "DisplayCollectionName";

                    foreach (var lcid in languages)
                    {
                        var collectionName = string.Empty;

                        if (entity.DisplayCollectionName != null)
                        {
                            var collectionNameLabel =
                                entity.DisplayCollectionName.LocalizedLabels.FirstOrDefault(l =>
                                                                                            l.LanguageCode == lcid);
                            if (collectionNameLabel != null)
                            {
                                collectionName = collectionNameLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = collectionName;
                    }
                }

                if (settings.ExportDescriptions)
                {
                    // Description
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        var description = string.Empty;

                        if (entity.Description != null)
                        {
                            var descriptionLabel =
                                entity.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (descriptionLabel != null)
                            {
                                description = descriptionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = description;
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorkbook file, IOrganizationService service, FormExportOption options, ExportSettings esettings)
        {
            settings = esettings;

            // 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;

                    Thread.Sleep(2000);
                }

                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);

                            // Specific for header
                            if (options.ExportFormFields)
                            {
                                var cellNodes = formXml.DocumentElement.SelectNodes("header/rows/row/cell");
                                foreach (XmlNode cellNode in cellNodes)
                                {
                                    ExtractField(cellNode, crmFormLabels, form, null, null, entity, lcid);
                                }
                            }

                            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
                            }

                            // Specific for footer
                            if (options.ExportFormFields)
                            {
                                var cellNodes = formXml.DocumentElement.SelectNodes("footer/rows/row/cell");
                                foreach (XmlNode cellNode in cellNodes)
                                {
                                    ExtractField(cellNode, crmFormLabels, form, null, null, entity, lcid);
                                }
                            }
                        }

                        #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);
                    }

                    RetrieveLocLabelsRequest  request;
                    RetrieveLocLabelsResponse response;

                    if (settings.ExportNames)
                    {
                        // Names
                        request = new RetrieveLocLabelsRequest
                        {
                            AttributeName = "name",
                            EntityMoniker = new EntityReference("systemform", form.Id)
                        };

                        response = (RetrieveLocLabelsResponse)service.Execute(request);
                        foreach (var locLabel in response.Label.LocalizedLabels)
                        {
                            crmForm.Names.Add(locLabel.LanguageCode, locLabel.Label);
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        // 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 = 0;
            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);
                    }
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// OptionSet Id;OptionSet Name;OptionSetValue;Type;LCID1;LCID2;...;LCIDX
        /// </example>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        /// <param name="service"></param>
        /// <param name="settings"></param>
        public void Export(List <int> languages, ExcelWorksheet sheet, IOrganizationService service, ExportSettings settings)
        {
            var line = 0;

            AddHeader(sheet, languages);

            var request  = new RetrieveAllOptionSetsRequest();
            var response = (RetrieveAllOptionSetsResponse)service.Execute(request);
            var omds     = response.OptionSetMetadata;

            if (settings.SolutionId != Guid.Empty)
            {
                var oids = service.GetSolutionComponentObjectIds(settings.SolutionId, 9); // 9 = Global OptionSets
                omds = omds.Where(o => oids.Contains(o.MetadataId ?? Guid.Empty)).ToArray();
            }

            foreach (var omd in omds)
            {
                int cell;
                if (omd is OptionSetMetadata oomd)
                {
                    foreach (var option in oomd.Options.OrderBy(o => o.Value))
                    {
                        if (settings.ExportNames)
                        {
                            line++;
                            cell = 0;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = (oomd.MetadataId ?? Guid.Empty).ToString("B");
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = oomd.Name;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                            foreach (var lcid in languages)
                            {
                                var label = string.Empty;

                                var optionLabel =
                                    option.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }

                                ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                            }
                        }

                        if (settings.ExportDescriptions)
                        {
                            line++;
                            cell = 0;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = (oomd.MetadataId ?? Guid.Empty).ToString("B");
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = oomd.Name;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                            foreach (var lcid in languages)
                            {
                                var label = string.Empty;

                                var optionDescription =
                                    option.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (optionDescription != null)
                                {
                                    label = optionDescription.Label;
                                }

                                ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                            }
                        }
                    }
                }
                else if (omd is BooleanOptionSetMetadata)
                {
                    var bomd = (BooleanOptionSetMetadata)omd;

                    if (settings.ExportNames)
                    {
                        line++;
                        cell = 0;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = (omd.MetadataId ?? Guid.Empty).ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.FalseOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bomd.FalseOption.Label != null)
                            {
                                var optionLabel =
                                    bomd.FalseOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = (omd.MetadataId ?? Guid.Empty).ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.FalseOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bomd.FalseOption.Description != null)
                            {
                                var optionLabel =
                                    bomd.FalseOption.Description.LocalizedLabels.FirstOrDefault(l =>
                                                                                                l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportNames)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = (omd.MetadataId ?? Guid.Empty).ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.TrueOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bomd.TrueOption.Label != null)
                            {
                                var optionLabel =
                                    bomd.TrueOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        line++;
                        cell = 0;

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = (omd.MetadataId ?? Guid.Empty).ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.TrueOption.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            if (bomd.TrueOption.Description != null)
                            {
                                var optionLabel =
                                    bomd.TrueOption.Description.LocalizedLabels.FirstOrDefault(l =>
                                                                                               l.LanguageCode == lcid);
                                if (optionLabel != null)
                                {
                                    label = optionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// attributeId;entityLogicalName;attributeLogicalName;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        /// <param name="settings"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, ExportSettings settings)
        {
            var line = 0;
            int cell;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                foreach (var attribute in entity.Attributes.OrderBy(a => a.LogicalName))
                {
                    if (attribute.AttributeType == null ||
                        attribute.AttributeType.Value == AttributeTypeCode.BigInt ||
                        attribute.AttributeType.Value == AttributeTypeCode.CalendarRules ||
                        attribute.AttributeType.Value == AttributeTypeCode.EntityName ||
                        attribute.AttributeType.Value == AttributeTypeCode.ManagedProperty ||
                        attribute.AttributeType.Value == AttributeTypeCode.Uniqueidentifier ||
                        attribute.AttributeType.Value == AttributeTypeCode.Virtual && !(attribute is MultiSelectPicklistAttributeMetadata) ||
                        attribute.AttributeOf != null ||
                        !attribute.MetadataId.HasValue ||
                        !attribute.IsRenameable.Value)
                    {
                        continue;
                    }

                    if (attribute.DisplayName != null && attribute.DisplayName.LocalizedLabels.All(l => string.IsNullOrEmpty(l.Label)))
                    {
                        continue;
                    }

                    // If derived attribute from calculated field, don't process it
                    if (attribute.LogicalName.EndsWith("_state"))
                    {
                        var baseName = attribute.LogicalName.Remove(attribute.LogicalName.Length - 6, 6);

                        if (entity.Attributes.Any(a => a.LogicalName == baseName) &&
                            entity.Attributes.Any(a => a.LogicalName == baseName + "_date"))
                        {
                            continue;
                        }
                    }
                    if (attribute.LogicalName.EndsWith("_date"))
                    {
                        var baseName = attribute.LogicalName.Remove(attribute.LogicalName.Length - 5, 5);

                        if (entity.Attributes.Any(a => a.LogicalName == baseName) &&
                            entity.Attributes.Any(a => a.LogicalName == baseName + "_state"))
                        {
                            continue;
                        }
                    }

                    if (settings.ExportNames)
                    {
                        // DisplayName
                        line++;
                        cell = 0;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "DisplayName";

                        foreach (var lcid in languages)
                        {
                            var displayName = string.Empty;

                            if (attribute.DisplayName != null)
                            {
                                var displayNameLabel =
                                    attribute.DisplayName.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (displayNameLabel != null)
                                {
                                    displayName = displayNameLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = displayName;
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        // Description
                        line++;
                        cell = 0;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var description = string.Empty;

                            if (attribute.Description != null)
                            {
                                var descriptionLabel = attribute.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                if (descriptionLabel != null)
                                {
                                    description = descriptionLabel.Label;
                                }
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = description;
                        }
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
示例#13
0
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// OptionSet Id;OptionSet Name;OptionSetValue;Type;LCID1;LCID2;...;LCIDX
        /// </example>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        /// <param name="service"></param>
        public void Export(List <int> languages, ExcelWorksheet sheet, IOrganizationService service)
        {
            var line = 1;

            AddHeader(sheet, languages);

            var request  = new RetrieveAllOptionSetsRequest();
            var response = (RetrieveAllOptionSetsResponse)service.Execute(request);

            foreach (var omd in response.OptionSetMetadata)
            {
                if (omd is OptionSetMetadata)
                {
                    var oomd = (OptionSetMetadata)omd;
                    foreach (var option in oomd.Options.OrderBy(o => o.Value))
                    {
                        var cell = 0;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            var optionLabel = option.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionLabel != null)
                            {
                                label = optionLabel.Label;
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }

                        line++;
                        cell = 0;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                        foreach (var lcid in languages)
                        {
                            var label = string.Empty;

                            var optionDescription = option.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionDescription != null)
                            {
                                label = optionDescription.Label;
                            }

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                        }

                        line++;
                    }
                }
                else if (omd is BooleanOptionSetMetadata)
                {
                    var bomd = (BooleanOptionSetMetadata)omd;

                    var cell = 0;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.FalseOption.Value;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                    foreach (var lcid in languages)
                    {
                        var label = string.Empty;

                        if (bomd.FalseOption.Label != null)
                        {
                            var optionLabel =
                                bomd.FalseOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionLabel != null)
                            {
                                label = optionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                    }

                    line++;
                    cell = 0;

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.FalseOption.Value;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        var label = string.Empty;

                        if (bomd.FalseOption.Description != null)
                        {
                            var optionLabel =
                                bomd.FalseOption.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionLabel != null)
                            {
                                label = optionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                    }

                    line++;
                    cell = 0;

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.TrueOption.Value;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                    foreach (var lcid in languages)
                    {
                        var label = string.Empty;

                        if (bomd.TrueOption.Label != null)
                        {
                            var optionLabel =
                                bomd.TrueOption.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionLabel != null)
                            {
                                label = optionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                    }

                    line++;
                    cell = 0;

                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.MetadataId.Value.ToString("B");
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = omd.Name;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = bomd.TrueOption.Value;
                    ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        var label = string.Empty;

                        if (bomd.TrueOption.Description != null)
                        {
                            var optionLabel =
                                bomd.TrueOption.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                            if (optionLabel != null)
                            {
                                label = optionLabel.Label;
                            }
                        }

                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                    }

                    line++;
                }
            }

            // Applying style to cells
            for (int i = 0; i < (4 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
示例#14
0
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// attributeId;entityLogicalName;attributeLogicalName;OptionSetValue;LCID1;LCID2;...;LCIDX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, ExportSettings settings)
        {
            var line = 0;
            var cell = 0;

            AddHeader(sheet, languages);

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                foreach (var attribute in entity.Attributes.OrderBy(a => a.LogicalName))
                {
                    if (attribute.AttributeType == null ||
                        attribute.AttributeType.Value != AttributeTypeCode.Picklist &&
                        attribute.AttributeType.Value != AttributeTypeCode.State &&
                        attribute.AttributeType.Value != AttributeTypeCode.Status &&
                        !(attribute is MultiSelectPicklistAttributeMetadata) ||
                        !attribute.MetadataId.HasValue)
                    {
                        continue;
                    }

                    OptionSetMetadata omd = null;

                    switch (attribute.AttributeType.Value)
                    {
                    case AttributeTypeCode.Picklist:
                        omd = ((PicklistAttributeMetadata)attribute).OptionSet;
                        break;

                    case AttributeTypeCode.State:
                        omd = ((StateAttributeMetadata)attribute).OptionSet;
                        break;

                    case AttributeTypeCode.Status:
                        omd = ((StatusAttributeMetadata)attribute).OptionSet;
                        break;

                    case AttributeTypeCode.Virtual:
                        omd = ((MultiSelectPicklistAttributeMetadata)attribute).OptionSet;
                        break;
                    }

                    if (omd.IsGlobal.Value)
                    {
                        continue;
                    }

                    foreach (var option in omd.Options.OrderBy(o => o.Value))
                    {
                        if (settings.ExportNames)
                        {
                            line++;
                            cell = 0;

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.AttributeType.Value.ToString();
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Label";

                            foreach (var lcid in languages)
                            {
                                var label = string.Empty;

                                if (option.Label != null)
                                {
                                    var optionLabel =
                                        option.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                    if (optionLabel != null)
                                    {
                                        label = optionLabel.Label;
                                    }
                                }

                                ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                            }
                        }

                        if (settings.ExportDescriptions)
                        {
                            line++;
                            cell = 0;

                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.MetadataId.Value.ToString("B");
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = entity.LogicalName;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.LogicalName;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = attribute.AttributeType.Value.ToString();
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = option.Value;
                            ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                            foreach (var lcid in languages)
                            {
                                var label = string.Empty;

                                if (option.Description != null)
                                {
                                    var optionLabel =
                                        option.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                                    if (optionLabel != null)
                                    {
                                        label = optionLabel.Label;
                                    }
                                }

                                ZeroBasedSheet.Cell(sheet, line, cell++).Value = label;
                            }
                        }
                    }
                }
            }

            // Applying style to cells
            for (int i = 0; i < (6 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i <= line; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// viewId;entityLogicalName;viewName;ViewType;Type;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="languages"></param>
        /// <param name="file"></param>
        /// <param name="service"></param>
        /// <param name="settings"></param>
        public void Export(List <int> languages, ExcelWorkbook file, IOrganizationService service, ExportSettings settings, ConnectionDetail detail)
        {
            var line = 0;
            int cell;

            siteMaps = GetSiteMaps(settings, service, detail);

            var areaSheet    = file.Worksheets.Add("SiteMap Areas");
            var groupSheet   = file.Worksheets.Add("SiteMap Groups");
            var subAreaSheet = file.Worksheets.Add("SiteMap SubAreas");

            AddAreaHeader(areaSheet, languages);
            AddGroupHeader(groupSheet, languages);
            AddSubAreaHeader(subAreaSheet, languages);

            var crmSiteMapAreas    = new List <CrmSiteMapArea>();
            var crmSiteMapGroups   = new List <CrmSiteMapGroup>();
            var crmSiteMapSubAreas = new List <CrmSiteMapSubArea>();

            foreach (var siteMap in siteMaps.Entities)
            {
                var siteMapDoc = new XmlDocument();
                siteMapDoc.LoadXml(siteMap["sitemapxml"].ToString());

                #region Export Area

                var areaNodes = siteMapDoc.SelectNodes("SiteMap/Area");
                foreach (XmlNode areaNode in areaNodes)
                {
                    var area = new CrmSiteMapArea
                    {
                        Id          = areaNode.Attributes["Id"].Value,
                        SiteMapId   = siteMap.Id,
                        SiteMapName = siteMap.GetAttributeValue <string>("sitemapname")
                    };

                    if (settings.ExportNames)
                    {
                        foreach (XmlNode titleNode in areaNode.SelectNodes("Titles/Title"))
                        {
                            area.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                            titleNode.Attributes["Title"].Value);
                        }
                    }

                    if (settings.ExportDescriptions)
                    {
                        foreach (XmlNode titleNode in areaNode.SelectNodes("Descriptions/Description"))
                        {
                            area.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                                  titleNode.Attributes["Description"].Value);
                        }
                    }

                    crmSiteMapAreas.Add(area);

                    #region Export Groups

                    var groupNodes = areaNode.SelectNodes("Group");
                    foreach (XmlNode groupNode in groupNodes)
                    {
                        var group = new CrmSiteMapGroup
                        {
                            Id          = groupNode.Attributes["Id"].Value,
                            AreaId      = areaNode.Attributes["Id"].Value,
                            SiteMapId   = siteMap.Id,
                            SiteMapName = siteMap.GetAttributeValue <string>("sitemapname")
                        };

                        if (settings.ExportNames)
                        {
                            foreach (XmlNode titleNode in groupNode.SelectNodes("Titles/Title"))
                            {
                                group.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                                 titleNode.Attributes["Title"].Value);
                            }
                        }

                        if (settings.ExportDescriptions)
                        {
                            foreach (XmlNode titleNode in groupNode.SelectNodes("Descriptions/Description"))
                            {
                                group.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                                       titleNode.Attributes["Description"].Value);
                            }
                        }

                        crmSiteMapGroups.Add(group);

                        #region Export SubArea

                        var subAreaNodes = groupNode.SelectNodes("SubArea");
                        foreach (XmlNode subAreaNode in subAreaNodes)
                        {
                            var subArea = new CrmSiteMapSubArea()
                            {
                                Id          = subAreaNode.Attributes["Id"].Value,
                                GroupId     = groupNode.Attributes["Id"].Value,
                                AreaId      = areaNode.Attributes["Id"].Value,
                                SiteMapId   = siteMap.Id,
                                SiteMapName = siteMap.GetAttributeValue <string>("sitemapname")
                            };

                            if (settings.ExportNames)
                            {
                                foreach (XmlNode titleNode in subAreaNode.SelectNodes("Titles/Title"))
                                {
                                    subArea.Titles.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                                       titleNode.Attributes["Title"].Value);
                                }
                            }

                            if (settings.ExportDescriptions)
                            {
                                foreach (XmlNode titleNode in subAreaNode.SelectNodes("Descriptions/Description"))
                                {
                                    subArea.Descriptions.Add(int.Parse(titleNode.Attributes["LCID"].Value),
                                                             titleNode.Attributes["Description"].Value);
                                }
                            }

                            crmSiteMapSubAreas.Add(subArea);
                        }

                        #endregion Export SubArea
                    }

                    #endregion Export Groups
                }

                #endregion Export Area
            }

            #region Area sheet

            foreach (var crmArea in crmSiteMapAreas)
            {
                if (settings.ExportNames)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.SiteMapName;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Id;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = "Title";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(areaSheet, line, cell++).Value =
                            crmArea.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }

                if (settings.ExportDescriptions)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.SiteMapName;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = crmArea.Id;
                    ZeroBasedSheet.Cell(areaSheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(areaSheet, line, cell++).Value =
                            crmArea.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }
            }

            #endregion Area sheet

            #region Group sheet

            line = 0;
            foreach (var crmGroup in crmSiteMapGroups)
            {
                if (settings.ExportNames)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.SiteMapName;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.AreaId;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Id;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = "Title";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(groupSheet, line, cell++).Value =
                            crmGroup.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }

                if (settings.ExportDescriptions)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.SiteMapName;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.AreaId;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = crmGroup.Id;
                    ZeroBasedSheet.Cell(groupSheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(groupSheet, line, cell++).Value =
                            crmGroup.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }
            }

            #endregion Group sheet

            #region SubArea sheet

            line = 0;
            foreach (var crmSubArea in crmSiteMapSubAreas)
            {
                if (settings.ExportNames)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.SiteMapName;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.AreaId;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.GroupId;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Id;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = "Title";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value =
                            crmSubArea.Titles.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }

                if (settings.ExportDescriptions)
                {
                    line++;
                    cell = 0;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.SiteMapName;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.SiteMapId.ToString();
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.AreaId;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.GroupId;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = crmSubArea.Id;
                    ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value = "Description";

                    foreach (var lcid in languages)
                    {
                        ZeroBasedSheet.Cell(subAreaSheet, line, cell++).Value =
                            crmSubArea.Descriptions.FirstOrDefault(n => n.Key == lcid).Value;
                    }
                }
            }

            #endregion SubArea sheet

            // Applying style to cells
            for (int i = 0; i < 4 + languages.Count; i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(areaSheet, 0, i).Style);
            }
            for (int i = 1; i <= crmSiteMapAreas.Select(c => c.Titles).Count() + crmSiteMapAreas.Select(c => c.Descriptions).Count(); i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(areaSheet, i, j).Style);
                }
            }

            for (int i = 0; i < 5 + languages.Count; i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(groupSheet, 0, i).Style);
            }

            for (int i = 1; i <= crmSiteMapGroups.Select(c => c.Titles).Count() + crmSiteMapGroups.Select(c => c.Descriptions).Count(); i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(groupSheet, i, j).Style);
                }
            }

            for (int i = 0; i < 6 + languages.Count; i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(subAreaSheet, 0, i).Style);
            }

            for (int i = 1; i <= crmSiteMapSubAreas.Select(c => c.Titles).Count() + crmSiteMapSubAreas.Select(c => c.Descriptions).Count(); i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(subAreaSheet, i, j).Style);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <example>
        /// visualizationId;entityLogicalName;visualizationName;LCID1;LCID2;...;LCODX
        /// </example>
        /// <param name="entities"></param>
        /// <param name="languages"></param>
        /// <param name="sheet"></param>
        public void Export(List <EntityMetadata> entities, List <int> languages, ExcelWorksheet sheet, IOrganizationService service)
        {
            var line = 1;

            AddHeader(sheet, languages);

            var crmVisualizations = new List <CrmVisualization>();

            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                if (!entity.MetadataId.HasValue)
                {
                    continue;
                }

                var visualizations = RetrieveVisualizations(entity.ObjectTypeCode.Value, service);

                foreach (var visualization in visualizations)
                {
                    var crmVisualization = crmVisualizations.FirstOrDefault(cv => cv.Id == visualization.Id);
                    if (crmVisualization == null)
                    {
                        crmVisualization = new CrmVisualization
                        {
                            Id           = visualization.Id,
                            Entity       = visualization.GetAttributeValue <string>("primaryentitytypecode"),
                            Names        = new Dictionary <int, string>(),
                            Descriptions = new Dictionary <int, string>()
                        };
                        crmVisualizations.Add(crmVisualization);
                    }

                    // Names
                    var request = new RetrieveLocLabelsRequest
                    {
                        AttributeName = "name",
                        EntityMoniker = new EntityReference("savedqueryvisualization", visualization.Id)
                    };

                    var response = (RetrieveLocLabelsResponse)service.Execute(request);
                    foreach (var locLabel in response.Label.LocalizedLabels)
                    {
                        crmVisualization.Names.Add(locLabel.LanguageCode, locLabel.Label);
                    }

                    // Descriptions
                    request = new RetrieveLocLabelsRequest
                    {
                        AttributeName = "description",
                        EntityMoniker = new EntityReference("savedqueryvisualization", visualization.Id)
                    };

                    response = (RetrieveLocLabelsResponse)service.Execute(request);
                    foreach (var locLabel in response.Label.LocalizedLabels)
                    {
                        crmVisualization.Descriptions.Add(locLabel.LanguageCode, locLabel.Label);
                    }
                }
            }

            foreach (var crmVisualization in crmVisualizations.OrderBy(cv => cv.Entity))
            {
                var cell = 0;
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmVisualization.Id.ToString("B");
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmVisualization.Entity;
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Name";

                foreach (var lcid in languages)
                {
                    var name = crmVisualization.Names.FirstOrDefault(n => n.Key == lcid);
                    if (name.Value != null)
                    {
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = name.Value;
                    }
                    else
                    {
                        cell++;
                    }
                }

                line++;
                cell = 0;
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmVisualization.Id.ToString("B");
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = crmVisualization.Entity;
                ZeroBasedSheet.Cell(sheet, line, cell++).Value = "Description";

                foreach (var lcid in languages)
                {
                    var desc = crmVisualization.Descriptions.FirstOrDefault(n => n.Key == lcid);
                    if (desc.Value != null)
                    {
                        ZeroBasedSheet.Cell(sheet, line, cell++).Value = desc.Value;
                    }
                    else
                    {
                        cell++;
                    }
                }
                line++;
            }

            // Applying style to cells
            for (int i = 0; i < (3 + languages.Count); i++)
            {
                StyleMutator.TitleCell(ZeroBasedSheet.Cell(sheet, 0, i).Style);
            }

            for (int i = 1; i < line; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    StyleMutator.HighlightedCell(ZeroBasedSheet.Cell(sheet, i, j).Style);
                }
            }
        }