public void ImportFormName(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;
            var requests   = new List <SetLocLabelsRequest>();

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var currentFormId = new Guid(ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());

                var request = new SetLocLabelsRequest
                {
                    EntityMoniker = new EntityReference("systemform", currentFormId),
                    AttributeName = ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Name" ? "name" : "description"
                };

                var labels = new List <LocalizedLabel>();

                var columnIndex = 4;
                while (columnIndex < cellsCount)
                {
                    if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        labels.Add(new LocalizedLabel(label, lcid));
                    }

                    columnIndex++;
                }

                request.Labels = labels.ToArray();

                requests.Add(request);
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);
        }
        public void ImportFormsContent(IOrganizationService service, List <Entity> forms, BackgroundWorker worker)
        {
            name = "Forms Contents";
            var arg = new TranslationProgressEventArgs();

            foreach (var form in forms)
            {
                AddRequest(new UpdateRequest {
                    Target = form
                });
                ExecuteMultiple(service, arg);
            }
            ExecuteMultiple(service, arg, true);
        }
        public void ImportFormsContent(IOrganizationService service, List <Entity> forms, BackgroundWorker worker)
        {
            name = "Forms Contents";

            OnLog(new LogEventArgs($"Importing {name} translations"));

            var setting         = GetCurrentUserSettings(service);
            var userSettingLcid = setting.GetAttributeValue <int>("uilanguageid");
            var currentSetting  = userSettingLcid;

            int orgLcid = GetCurrentOrgBaseLanguage(service);

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

                Thread.Sleep(2000);
            }

            var arg = new TranslationProgressEventArgs();

            foreach (var form in forms)
            {
                AddRequest(new UpdateRequest {
                    Target = form
                });
                ExecuteMultiple(service, arg, forms.Count);
            }
            ExecuteMultiple(service, arg, forms.Count, true);

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

                Thread.Sleep(2000);
            }
        }
        public void Import(IOrganizationService service)
        {
            var arg = new TranslationProgressEventArgs {
                SheetName = "SiteMaps"
            };

            foreach (var siteMap in siteMaps.Entities)
            {
                if (siteMapsToTranslate.Contains(siteMap.Id))
                {
                    AddRequest(new UpdateRequest {
                        Target = siteMap
                    });
                    //ExecuteMultiple(service, arg);
                }
            }

            ExecuteMultiple(service, arg, true);
        }
Пример #5
0
        public void ImportFormsContent(IOrganizationService service, List <Entity> forms, BackgroundWorker worker)
        {
            name = "Dashboards contents";

            OnLog(new LogEventArgs($"Importing {name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = "Forms"
            };

            foreach (var form in forms)
            {
                AddRequest(new UpdateRequest {
                    Target = form
                });
                ExecuteMultiple(service, arg, forms.Count);
            }
            ExecuteMultiple(service, arg, forms.Count, true);
        }
Пример #6
0
 public virtual void OnResult(TranslationProgressEventArgs e)
 {
     Result?.Invoke(this, e);
 }
Пример #7
0
        public void Import(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;
            var requests   = new List <SetLocLabelsRequest>();

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                if (HasEmptyCells(sheet, rowI, 2))
                {
                    continue;
                }

                var currentVisualizationId = new Guid(ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString());

                var locLabel = ((RetrieveLocLabelsResponse)service.Execute(new RetrieveLocLabelsRequest
                {
                    EntityMoniker = new EntityReference("savedqueryvisualization", currentVisualizationId),
                    AttributeName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() == "Name" ? "name" : "description"
                })).Label;

                var request = new SetLocLabelsRequest
                {
                    EntityMoniker = new EntityReference("savedqueryvisualization", currentVisualizationId),
                    AttributeName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() == "Name" ? "name" : "description"
                };

                var labels = locLabel.LocalizedLabels.ToList();

                var columnIndex = 3;
                while (columnIndex < cellsCount)
                {
                    if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = labels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            labels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }
                    }

                    columnIndex++;
                }

                request.Labels = labels.ToArray();

                requests.Add(request);
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);
        }
Пример #8
0
        public void Import(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                UpdateOptionValueRequest request = requests.FirstOrDefault(r => r.OptionSetName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());
                if (request == null)
                {
                    request = new UpdateOptionValueRequest
                    {
                        AttributeLogicalName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString(),
                        EntityLogicalName    = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        Value       = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString()),
                        Label       = new Label(),
                        Description = new Label(),
                        MergeLabels = true
                    };

                    int columnIndex = 5;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Label")
                    {
                        while (columnIndex < cellsCount)
                        {
                            if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                            {
                                var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                                var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();
                                request.Label.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                            }

                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Description")
                    {
                        while (columnIndex < cellsCount)
                        {
                            if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                            {
                                var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                                var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();
                                request.Description.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                            }

                            columnIndex++;
                        }
                    }

                    requests.Add(request);
                }
                else
                {
                    int columnIndex = 5;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Label")
                    {
                        while (columnIndex < cellsCount)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Label.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Description")
                    {
                        while (columnIndex < cellsCount)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Description.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }
                }
            }

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg);
            }

            ExecuteMultiple(service, arg, true);
        }
        public void Import(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs {
                Message = "Started global optionset import", Type = LogType.Info
            });
            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount = sheet.Dimension.Rows;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                UpdateOptionValueRequest request = requests.FirstOrDefault(r => r.OptionSetName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString() &&
                                                                           r.Value == int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString()));
                if (request == null)
                {
                    request = new UpdateOptionValueRequest
                    {
                        OptionSetName = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        Value         = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString()),
                        Label         = new Label(),
                        Description   = new Label(),
                        MergeLabels   = true
                    };

                    int columnIndex = 4;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Label")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                var ll = request.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == int.Parse(sLcid));
                                if (ll != null)
                                {
                                    ll.Label = sLabel;
                                }
                                else
                                {
                                    request.Label.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                                }
                            }

                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Description")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                var ll = request.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == int.Parse(sLcid));
                                if (ll != null)
                                {
                                    ll.Label = sLabel;
                                }
                                else
                                {
                                    request.Description.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                                }
                            }

                            columnIndex++;
                        }
                    }

                    requests.Add(request);
                }
                else
                {
                    int columnIndex = 4;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Label")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                var ll = request.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == int.Parse(sLcid));
                                if (ll != null)
                                {
                                    ll.Label = sLabel;
                                }
                                else
                                {
                                    request.Label.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                                }
                            }
                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Description")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                var ll = request.Description.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == int.Parse(sLcid));
                                if (ll != null)
                                {
                                    ll.Label = sLabel;
                                }
                                else
                                {
                                    request.Description.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                                }
                            }
                            columnIndex++;
                        }
                    }
                }
            }

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                //ExecuteMultiple(service, arg);
            }
            ExecuteMultiple(service, arg, true);
            OnLog(new LogEventArgs {
                Message = "Completed global optionset import", Type = LogType.Info
            });
        }
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var rmds = new List <ManyToManyRelationshipMetadata>();

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var rmd = rmds.FirstOrDefault(r => r.MetadataId == new Guid(ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString()));
                if (rmd == null)
                {
                    var currentEntity = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString());
                    if (currentEntity == null)
                    {
                        var request = new RetrieveEntityRequest
                        {
                            LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString(),
                            EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                        };

                        var response = ((RetrieveEntityResponse)service.Execute(request));
                        currentEntity = response.EntityMetadata;

                        emds.Add(currentEntity);
                    }

                    rmd = currentEntity.ManyToManyRelationships.FirstOrDefault(r => r.IntersectEntityName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString());
                    rmds.Add(rmd);
                }

                if (rmd == null)
                {
                    OnLog(new LogEventArgs
                    {
                        Type    = LogType.Warning,
                        Message = $"Unable to find relationship '{ZeroBasedSheet.Cell(sheet, rowI, 2).Value}' for entity '{ZeroBasedSheet.Cell(sheet, rowI, 0).Value}"
                    });
                    continue;
                }

                int columnIndex = 3;

                if (rmd.Entity1LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString())
                {
                    rmd.Entity1AssociatedMenuConfiguration.Label = new Label();

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            rmd.Entity1AssociatedMenuConfiguration.Label.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }

                        columnIndex++;
                    }
                }
                else if (rmd.Entity2LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString())
                {
                    rmd.Entity2AssociatedMenuConfiguration.Label = new Label();

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            rmd.Entity2AssociatedMenuConfiguration.Label.LocalizedLabels.Add(new LocalizedLabel(label,
                                                                                                                lcid));
                        }
                        columnIndex++;
                    }
                }
                else
                {
                    OnLog(new LogEventArgs
                    {
                        Type    = LogType.Warning,
                        Message = $"Unable to find entity '{ZeroBasedSheet.Cell(sheet, rowI, 0).Value}' in relationship '{ZeroBasedSheet.Cell(sheet, rowI, 2).Value}"
                    });
                }
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var rmd in rmds)
            {
                var request = new UpdateRelationshipRequest
                {
                    Relationship = rmd,
                    MergeLabels  = true
                };

                AddRequest(request);
                ExecuteMultiple(service, arg, rmds.Count);
            }
            ExecuteMultiple(service, arg, rmds.Count, true);
        }
Пример #11
0
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            var rmds = new List <OneToManyRelationshipMetadata>();

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var rmd = rmds.FirstOrDefault(r => ZeroBasedSheet.Cell(sheet, rowI, 1).Value != null && r.MetadataId == new Guid(ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString()));
                if (rmd == null)
                {
                    var currentEntity = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 0).Value?.ToString());
                    if (currentEntity == null)
                    {
                        var request = new RetrieveEntityRequest
                        {
                            LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString(),
                            EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                        };

                        var response = (RetrieveEntityResponse)service.Execute(request);
                        currentEntity = response.EntityMetadata;

                        emds.Add(currentEntity);
                    }
                    rmd =
                        currentEntity.OneToManyRelationships.FirstOrDefault(
                            r => r.SchemaName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value?.ToString());
                    if (rmd == null)
                    {
                        rmd =
                            currentEntity.ManyToOneRelationships.FirstOrDefault(
                                r => r.SchemaName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value?.ToString());
                    }

                    rmds.Add(rmd);
                }

                int columnIndex = 4;
                if (rmd.AssociatedMenuConfiguration.Label == null)
                {
                    rmd.AssociatedMenuConfiguration.Label = new Label();
                }

                while (columnIndex < cellsCount)
                {
                    if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();
                        var ll    = rmd.AssociatedMenuConfiguration.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == lcid);
                        if (ll != null)
                        {
                            ll.Label = label;
                        }
                        else
                        {
                            rmd.AssociatedMenuConfiguration.Label.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }
                    }

                    columnIndex++;
                }
            }

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var rmd in rmds)
            {
                var request = new UpdateRelationshipRequest
                {
                    Relationship = rmd,
                    MergeLabels  = true
                };

                AddRequest(request);
                //ExecuteMultiple(service, arg);
            }

            ExecuteMultiple(service, arg, true);
        }
Пример #12
0
        public void Import(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount = sheet.Dimension.Rows;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                UpdateOptionValueRequest request =
                    requests
                    .FirstOrDefault(
                        r => r.OptionSetName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString() &&
                        r.Value == int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString()));

                if (request == null)
                {
                    request = new UpdateOptionValueRequest
                    {
                        AttributeLogicalName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString(),
                        EntityLogicalName    = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        Value       = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString()),
                        Label       = new Label(),
                        Description = new Label(),
                        MergeLabels = true
                    };

                    int columnIndex = 6;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Label")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Label.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Description")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Description.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }

                    requests.Add(request);
                }
                else
                {
                    int columnIndex = 6;

                    if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Label")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Label.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }
                    else if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Description")
                    {
                        // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                        while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var sLcid  = ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString();
                            var sLabel = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            if (sLcid.Length > 0 && sLabel.Length > 0)
                            {
                                request.Description.LocalizedLabels.Add(new LocalizedLabel(sLabel, int.Parse(sLcid)));
                            }
                            columnIndex++;
                        }
                    }
                }
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);
        }
Пример #13
0
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount = sheet.Dimension.Rows;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                if (HasEmptyCells(sheet, rowI, 5))
                {
                    continue;
                }

                var value = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString());

                var emd = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());
                if (emd == null)
                {
                    var mdRequest = new RetrieveEntityRequest
                    {
                        LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                    };

                    var response = ((RetrieveEntityResponse)service.Execute(mdRequest));
                    emd = response.EntityMetadata;

                    emds.Add(emd);
                }

                var            amd    = emd.Attributes.FirstOrDefault(a => a.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString());
                OptionMetadata option = null;
                if (amd is PicklistAttributeMetadata pamd)
                {
                    option = pamd.OptionSet.Options.FirstOrDefault(o => o.Value == value);
                }
                else if (amd is StateAttributeMetadata samd)
                {
                    option = samd.OptionSet.Options.FirstOrDefault(o => o.Value == value);
                }
                else if (amd is StatusAttributeMetadata ssamd)
                {
                    option = ssamd.OptionSet.Options.FirstOrDefault(o => o.Value == value);
                }

                if (option == null)
                {
                    OnLog(new LogEventArgs($"Unable to determine type of the AttributeMetadata for attribute {amd.LogicalName}")
                    {
                        Type = LogType.Error
                    });
                    continue;
                }

                UpdateOptionValueRequest request =
                    requests
                    .FirstOrDefault(
                        r => r.OptionSetName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString() &&
                        r.Value == value);

                if (request == null)
                {
                    request = new UpdateOptionValueRequest
                    {
                        AttributeLogicalName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString(),
                        EntityLogicalName    = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        Value       = value,
                        Label       = option.Label ?? new Label(),
                        Description = option.Description ?? new Label(),
                        MergeLabels = true
                    };

                    requests.Add(request);
                }

                int columnIndex = 6;

                if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Label")
                {
                    // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                    while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = request.Label.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            request.Label.LocalizedLabels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }
                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 5).Value.ToString() == "Description")
                {
                    // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                    while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = request.Description.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            request.Description.LocalizedLabels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }
                        columnIndex++;
                    }
                }
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);
        }
        public void Import(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount = sheet.Dimension.Rows;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                if (HasEmptyCells(sheet, rowI, 3))
                {
                    continue;
                }

                var value = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString());

                UpdateOptionValueRequest request = requests.FirstOrDefault(r => r.OptionSetName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString() &&
                                                                           r.Value == value);

                if (request == null)
                {
                    var md = ((RetrieveOptionSetResponse)service.Execute(new RetrieveOptionSetRequest
                    {
                        Name = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        RetrieveAsIfPublished = true
                    })).OptionSetMetadata;

                    if (md is OptionSetMetadata osm)
                    {
                        var option = osm.Options.FirstOrDefault(o => o.Value == value);

                        request = new UpdateOptionValueRequest
                        {
                            OptionSetName = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                            Value         = value,
                            Label         = option?.Label ?? new Label(),
                            Description   = option?.Description ?? new Label(),
                            MergeLabels   = true
                        };
                    }
                    else if (md is BooleanOptionSetMetadata bosm)
                    {
                        var option = value == 0 ? bosm.FalseOption : bosm.TrueOption;

                        request = new UpdateOptionValueRequest
                        {
                            OptionSetName = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                            Value         = value,
                            Label         = option?.Label ?? new Label(),
                            Description   = option?.Description ?? new Label(),
                            MergeLabels   = true
                        };
                    }

                    requests.Add(request);
                }

                int columnIndex = 4;

                if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Label")
                {
                    // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                    while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = request.Label.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            request.Label.LocalizedLabels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }

                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Description")
                {
                    // WTF: QUESTIONABLE DELETION: row.Cells.Count() > columnIndex &&
                    while (ZeroBasedSheet.Cell(sheet, rowI, columnIndex) != null && ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = request.Description.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            request.Description.LocalizedLabels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }

                        columnIndex++;
                    }
                }
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);
        }
Пример #15
0
        protected void ExecuteMultiple(IOrganizationService service, TranslationProgressEventArgs e, int total, bool forceUpdate = false)
        {
            if (request == null)
            {
                return;
            }
            if (request.Requests.Count % BulkCount != 0 && forceUpdate == false)
            {
                return;
            }

            e.SheetName = name;
            //e.TotalItems += request.Requests.Count;
            e.TotalItems += total;

            OnResult(e);

            var bulkResponse = (ExecuteMultipleResponse)service.Execute(request);

            if (bulkResponse.IsFaulted)
            {
                e.FailureCount += bulkResponse.Responses.Count(r => r.Fault != null);
                e.SuccessCount += request.Requests.Count - bulkResponse.Responses.Count;

                foreach (var response in bulkResponse.Responses)
                {
                    if (response.Fault != null)
                    {
                        string message;
                        var    faultIndex   = response.RequestIndex;
                        var    faultRequest = request.Requests[faultIndex];

                        if (faultRequest is UpdateRequest ur)
                        {
                            message =
                                $"Error while updating record {ur.Target.LogicalName} ({ur.Target.Id}): {response.Fault.Message}";
                        }
                        else if (faultRequest is UpdateAttributeRequest uar)
                        {
                            message =
                                $"Error while updating attribute {uar.Attribute.LogicalName}: {response.Fault.Message}";
                        }
                        else if (faultRequest is UpdateRelationshipRequest urr)
                        {
                            message =
                                $"Error while updating relationship {urr.Relationship.SchemaName}: {response.Fault.Message}";
                        }
                        else if (faultRequest is UpdateOptionSetRequest uosr)
                        {
                            message =
                                $"Error while updating optionset {uosr.OptionSet.Name}: {response.Fault.Message}";
                        }
                        else if (faultRequest is UpdateOptionValueRequest uovr)
                        {
                            if (!string.IsNullOrEmpty(uovr.OptionSetName))
                            {
                                message =
                                    $"Error while updating global optionset ({uovr.OptionSetName}) value ({uovr.Value}) label: {response.Fault.Message}";
                            }
                            else
                            {
                                message =
                                    $"Error while updating option ({uovr.Value}) label for attribute {uovr.AttributeLogicalName} ({uovr.EntityLogicalName}): {response.Fault.Message}";
                            }
                        }
                        else if (faultRequest is SetLocLabelsRequest sllr)
                        {
                            message =
                                $"Error while updating {sllr.AttributeName} of record {sllr.EntityMoniker.LogicalName} ({sllr.EntityMoniker.Id}): {response.Fault.Message}";
                        }
                        else
                        {
                            message = response.Fault.Message;
                        }

                        OnLog(new LogEventArgs(message)
                        {
                            Type = LogType.Error
                        });
                    }
                }
            }
            else
            {
                e.SuccessCount += request.Requests.Count;
            }

            OnResult(e);

            InitMultipleRequest();
        }
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var emd = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());
                if (emd == null)
                {
                    var request = new RetrieveEntityRequest
                    {
                        LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                    };

                    var response = ((RetrieveEntityResponse)service.Execute(request));
                    emd = response.EntityMetadata;

                    emds.Add(emd);
                }

                if (ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() == "DisplayName")
                {
                    emd.DisplayName = new Label();
                    int columnIndex = 3;

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            emd.DisplayName.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }

                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() == "DisplayCollectionName")
                {
                    emd.DisplayCollectionName = new Label();
                    int columnIndex = 3;

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            emd.DisplayCollectionName.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }

                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() == "Description")
                {
                    emd.Description = new Label();
                    int columnIndex = 3;

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                            emd.Description.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }

                        columnIndex++;
                    }
                }
            }

            var entities = emds.Where(e => e.IsRenameable.Value).ToList();

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var emd in entities)
            {
                var entityUpdate = new EntityMetadata();
                entityUpdate.LogicalName           = emd.LogicalName;
                entityUpdate.DisplayName           = emd.DisplayName;
                entityUpdate.Description           = emd.Description;
                entityUpdate.DisplayCollectionName = emd.DisplayCollectionName;

                AddRequest(new UpdateEntityRequest {
                    Entity = entityUpdate, MergeLabels = true
                });
                ExecuteMultiple(service, arg, entities.Count);
            }

            ExecuteMultiple(service, arg, entities.Count, true);
        }
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            var amds = new List <MasterAttribute>();

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var amd = amds.FirstOrDefault(a => a.Amd.MetadataId == new Guid(ZeroBasedSheet.Cell(sheet, rowI, 0).Value.ToString()));
                if (amd == null)
                {
                    var currentEntity = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());
                    if (currentEntity == null)
                    {
                        var request = new RetrieveEntityRequest
                        {
                            LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                            EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                        };

                        var response = ((RetrieveEntityResponse)service.Execute(request));
                        currentEntity = response.EntityMetadata;

                        emds.Add(currentEntity);
                    }

                    amd     = new MasterAttribute();
                    amd.Amd = currentEntity.Attributes.FirstOrDefault(a => string.Equals(a.LogicalName,
                                                                                         ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString(), StringComparison.OrdinalIgnoreCase));

                    //still null? someone deleted the attribute while we were busy translating. Let's skip it!
                    if (amd.Amd == null)
                    {
                        OnLog(new LogEventArgs
                        {
                            Type    = LogType.Warning,
                            Message = $"Attribute {ZeroBasedSheet.Cell(sheet, rowI, 1).Value} - {ZeroBasedSheet.Cell(sheet, rowI, 2).Value} is missing in CRM!"
                        });
                        continue;
                    }

                    amds.Add(amd);
                }

                int columnIndex = 4;

                if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "DisplayName")
                {
                    amd.Amd.DisplayName = new Label();

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();
                            amd.Amd.DisplayName.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }
                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Description")
                {
                    amd.Amd.Description = new Label();

                    while (columnIndex < cellsCount)
                    {
                        if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                        {
                            var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                            var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();
                            amd.Amd.Description.LocalizedLabels.Add(new LocalizedLabel(label, lcid));
                        }

                        columnIndex++;
                    }
                }
            }

            int i   = 0;
            var arg = new TranslationProgressEventArgs();

            foreach (var amd in amds)
            {
                if (amd.Amd.DisplayName.LocalizedLabels.All(l => string.IsNullOrEmpty(l.Label)) ||
                    amd.Amd.IsRenameable.Value == false)
                {
                    continue;
                }

                AddRequest(new UpdateAttributeRequest
                {
                    Attribute   = amd.Amd,
                    EntityName  = amd.Amd.EntityLogicalName,
                    MergeLabels = true
                });

                ExecuteMultiple(service, arg);
            }
            ExecuteMultiple(service, arg, true);
        }
Пример #18
0
        public void Import(ExcelWorksheet sheet, List <EntityMetadata> emds, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var requests = new List <UpdateOptionValueRequest>();

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                if (HasEmptyCells(sheet, rowI, 4))
                {
                    continue;
                }

                var value = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString());

                UpdateOptionValueRequest request = requests.FirstOrDefault(r => r.EntityLogicalName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString() &&
                                                                           r.AttributeLogicalName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString() &&
                                                                           r.Value == value);

                if (request == null)
                {
                    var emd = emds.FirstOrDefault(e => e.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());
                    if (emd == null)
                    {
                        var mdRequest = new RetrieveEntityRequest
                        {
                            LogicalName   = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                            EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships
                        };

                        var response = ((RetrieveEntityResponse)service.Execute(mdRequest));
                        emd = response.EntityMetadata;

                        emds.Add(emd);
                    }

                    var amd       = (BooleanAttributeMetadata)emd.Attributes.FirstOrDefault(a => a.LogicalName == ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString());
                    var dLabel    = (value == 0 ? amd?.OptionSet.FalseOption.Label : amd?.OptionSet.TrueOption.Label) ?? new Label();
                    var descLabel = (value == 0 ? amd?.OptionSet.FalseOption.Description : amd?.OptionSet.TrueOption.Description) ?? new Label();

                    request = new UpdateOptionValueRequest
                    {
                        AttributeLogicalName = ZeroBasedSheet.Cell(sheet, rowI, 2).Value.ToString(),
                        EntityLogicalName    = ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString(),
                        Value       = int.Parse(ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString()),
                        Label       = dLabel,
                        Description = descLabel,
                        MergeLabels = true
                    };

                    requests.Add(request);
                }

                int columnIndex = 5;

                if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Label")
                {
                    while (columnIndex < cellsCount)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        if (label.Length > 0)
                        {
                            var translatedLabel = request.Label.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                            if (translatedLabel == null)
                            {
                                translatedLabel = new LocalizedLabel(label, lcid);
                                request.Label.LocalizedLabels.Add(translatedLabel);
                            }
                            else
                            {
                                translatedLabel.Label = label;
                            }
                        }
                        columnIndex++;
                    }
                }
                else if (ZeroBasedSheet.Cell(sheet, rowI, 4).Value.ToString() == "Description")
                {
                    while (columnIndex < cellsCount)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        if (label.Length > 0)
                        {
                            var translatedLabel = request.Description.LocalizedLabels.FirstOrDefault(x => x.LanguageCode == lcid);
                            if (translatedLabel == null)
                            {
                                translatedLabel = new LocalizedLabel(label, lcid);
                                request.Description.LocalizedLabels.Add(translatedLabel);
                            }
                            else
                            {
                                translatedLabel.Label = label;
                            }
                        }
                        columnIndex++;
                    }
                }
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }

            ExecuteMultiple(service, arg, requests.Count, true);
        }
        public void ImportFormName(ExcelWorksheet sheet, IOrganizationService service, BackgroundWorker worker)
        {
            OnLog(new LogEventArgs($"Reading {sheet.Name}"));

            var rowsCount  = sheet.Dimension.Rows;
            var cellsCount = sheet.Dimension.Columns;
            var requests   = new List <SetLocLabelsRequest>();

            for (var rowI = 1; rowI < rowsCount; rowI++)
            {
                var currentFormId = new Guid(ZeroBasedSheet.Cell(sheet, rowI, 1).Value.ToString());

                var locLabel = ((RetrieveLocLabelsResponse)service.Execute(new RetrieveLocLabelsRequest
                {
                    EntityMoniker = new EntityReference("systemform", currentFormId),
                    AttributeName = ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Name" ? "name" : "description"
                })).Label;

                var labels = locLabel.LocalizedLabels.ToList();

                var request = new SetLocLabelsRequest
                {
                    EntityMoniker = new EntityReference("systemform", currentFormId),
                    AttributeName = ZeroBasedSheet.Cell(sheet, rowI, 3).Value.ToString() == "Name" ? "name" : "description",
                    Labels        = locLabel.LocalizedLabels.ToArray()
                };

                var columnIndex = 4;
                while (columnIndex < cellsCount)
                {
                    if (ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value != null)
                    {
                        var lcid  = int.Parse(ZeroBasedSheet.Cell(sheet, 0, columnIndex).Value.ToString());
                        var label = ZeroBasedSheet.Cell(sheet, rowI, columnIndex).Value.ToString();

                        var translatedLabel = labels.FirstOrDefault(x => x.LanguageCode == lcid);
                        if (translatedLabel == null)
                        {
                            translatedLabel = new LocalizedLabel(label, lcid);
                            labels.Add(translatedLabel);
                        }
                        else
                        {
                            translatedLabel.Label = label;
                        }
                    }

                    columnIndex++;
                }

                request.Labels = labels.ToArray();

                requests.Add(request);
            }

            OnLog(new LogEventArgs($"Importing {sheet.Name} translations"));

            var setting         = GetCurrentUserSettings(service);
            var userSettingLcid = setting.GetAttributeValue <int>("uilanguageid");
            var currentSetting  = userSettingLcid;

            int orgLcid = GetCurrentOrgBaseLanguage(service);

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

                Thread.Sleep(2000);
            }

            var arg = new TranslationProgressEventArgs {
                SheetName = sheet.Name
            };

            foreach (var request in requests)
            {
                AddRequest(request);
                ExecuteMultiple(service, arg, requests.Count);
            }
            ExecuteMultiple(service, arg, requests.Count, true);

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

                Thread.Sleep(2000);
            }
        }