public override void ExecuteExtention(CustomisationExporterRequest request,
                                       CustomisationExporterResponse response,
                                       ServiceRequestController controller)
 {
     response.Folder = request.SaveToFolder.FolderPath;
     controller.LogLiteral("Loading Metadata");
     ProcessForEntities(request, response, controller.Controller);
     ProcessForFields(request, response, controller.Controller);
     ProcessForRelationships(request, response, controller.Controller);
     ProcessForOptionSets(request, response, controller.Controller);
 }
예제 #2
0
        public override void ExecuteExtention(CustomisationExporterRequest request,
                                              CustomisationExporterResponse response,
                                              ServiceRequestController controller)
        {
            response.Folder = request.SaveToFolder.FolderPath;
            controller.LogLiteral("Loading Metadata");

            ProcessForEntities(request, response, controller.Controller);

            if ((request.Fields || request.FieldOptionSets) && request.IncludeAllRecordTypes)
            {
                controller.UpdateProgress(0, 1, "Loading All Fields.....");
                Service.LoadFieldsForAllEntities();
            }
            if ((request.Fields || request.Relationships) && request.IncludeAllRecordTypes)
            {
                controller.UpdateProgress(0, 1, "Loading All Relationships.....");
                Service.LoadRelationshipsForAllEntities();
            }
            ProcessForFields(request, response, controller.Controller);

            ProcessForRelationships(request, response, controller.Controller);
            ProcessForOptionSets(request, response, controller.Controller);

            response.Folder = request.SaveToFolder.FolderPath;

            if (request.Format == CustomisationExporterRequest.FileFormat.Xlsx)
            {
                var excelFileName = "Customisation Export " + DateTime.Now.ToFileTime() + ".xlsx";
                ExcelUtility.CreateXlsx(request.SaveToFolder.FolderPath, excelFileName, response.GetListsToOutput());
                response.ExcelFileName = excelFileName;
            }
            else
            {
                foreach (var item in response.GetListsToOutput())
                {
                    var csvName = item.Key + " " + DateTime.Now.ToFileTime() + ".csv";
                    CsvUtility.CreateCsv(request.SaveToFolder.FolderPath, csvName, item.Value);
                }
            }

            response.Message = "The Export is Complete";
        }
예제 #3
0
        public override void ExecuteExtention(RecordCountsRequest request,
                                              RecordCountsResponse response,
                                              ServiceRequestController controller)
        {
            controller.LogLiteral("Loading Types");

            var excludeTheseTypes = new[] { Entities.msdyn_componentlayer, Entities.msdyn_solutioncomponentsummary, Entities.msdyn_nonrelationalds, Entities.datalakeworkspace, Entities.datalakeworkspacepermission };

            var recordTypes = request.AllRecordTypes
                ? Service.GetAllRecordTypes()
                              .Except(excludeTheseTypes)
                              .Where(r => Service.GetRecordTypeMetadata(r).Searchable)
                              .OrderBy(n => Service.GetDisplayName(n))
                              .ToArray()
                : request.RecordTypes.Select(p => p.RecordType.Key).ToArray();

            if (request.OnlyIncludeSelectedOwner)
            {
                recordTypes = recordTypes.Where(r => Service.GetRecordTypeMetadata(r).HasOwner).ToArray();
            }

            var numberOfTypes          = recordTypes.Count();
            var numberOfTypesCompleted = 0;

            var noOwnerIndex       = "Organisation";
            var recordCountsByUser = new Dictionary <string, Dictionary <string, long> >();

            //due to limitations in crm web service cannot do a standard aggregate
            //as they are limitied to 50K records OOTB
            //need to query all records and manually aggregate
            foreach (var recordType in recordTypes)
            {
                var thisDictionary = new Dictionary <string, long>();

                try
                {
                    var metadata = Service.GetRecordTypeMetadata(recordType);
                    var hasOwner = metadata.HasOwner;
                    if (!hasOwner)
                    {
                        thisDictionary.Add(noOwnerIndex, 0);
                    }
                    long totalThisIteration = 0;
                    var  func = hasOwner
                        ? ((records) =>
                    {
                        totalThisIteration += records.Count();
                        controller.UpdateProgress(numberOfTypesCompleted, numberOfTypes, string.Format("Counting {0} ({1})", recordType, totalThisIteration));
                        foreach (var record in records)
                        {
                            var ownerType = record.GetLookupType("ownerid");
                            var ownerId = record.GetLookupId("ownerid");
                            var format = string.Format("{0}:{1}", ownerType, ownerId);
                            if (!thisDictionary.ContainsKey(format))
                            {
                                thisDictionary.Add(format, 0);
                            }
                            thisDictionary[format]++;
                        }
                    })
                        : (Action <IEnumerable <IRecord> >)((records) =>
                    {
                        totalThisIteration += records.Count();
                        controller.UpdateProgress(numberOfTypesCompleted, numberOfTypes, string.Format("Counting {0} ({1})", recordType, totalThisIteration));
                        foreach (var record in records)
                        {
                            thisDictionary[noOwnerIndex]++;
                        }
                    });
                    var fields = hasOwner
                        ? new[] { "ownerid" }
                        : new string[0];
                    var conditions = request.OnlyIncludeSelectedOwner
                        ? new[] { new Condition("ownerid", ConditionType.Equal, request.Owner.Id) }
                        : new Condition[0];

                    var query  = new QueryDefinition(recordType);
                    var filter = new Filter();
                    filter.Conditions.AddRange(conditions);
                    query.RootFilter.SubFilters.Add(filter);
                    query.Fields = fields;
                    Service.ProcessResults(query, func);
                    recordCountsByUser.Add(recordType, thisDictionary);
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new RecordCountsResponseItem(recordType, "Error Generating Counts", ex));
                }
                finally
                {
                    numberOfTypesCompleted++;
                }
            }
            controller.LogLiteral("Generating CSV files");

            var groupByOwner = request.GroupCountsByOwner ||
                               request.OnlyIncludeSelectedOwner;

            if (!groupByOwner)
            {
                var totals = new List <RecordCount>();
                foreach (var dictionary in recordCountsByUser)
                {
                    totals.Add(new RecordCount(Service.GetDisplayName(dictionary.Key), dictionary.Value.Sum(kv => kv.Value)));
                }
                response.RecordCounts = totals;
            }
            if (groupByOwner)
            {
                var teamNameField = Service.GetRecordTypeMetadata(Entities.team).PrimaryFieldSchemaName;
                var userNameField = Service.GetRecordTypeMetadata(Entities.systemuser).PrimaryFieldSchemaName;

                var allDistinctOwners = recordCountsByUser
                                        .SelectMany(kv => kv.Value.Select(kv2 => kv2.Key))
                                        .Distinct();
                var allTeams = allDistinctOwners
                               .Where(s => s.StartsWith(Entities.team))
                               .Distinct();
                var allUsers = allDistinctOwners
                               .Where(s => s.StartsWith(Entities.systemuser))
                               .Distinct();

                var ownerKeyLabels = new Dictionary <string, string>();
                var teams          = Service.RetrieveAll(Entities.team, new[] { teamNameField });
                foreach (var team in teams)
                {
                    ownerKeyLabels.Add(string.Format("{0}:{1}", Entities.team, team.Id), team.GetStringField(teamNameField));
                }
                var users = Service.RetrieveAll(Entities.systemuser, null);
                foreach (var user in users)
                {
                    ownerKeyLabels.Add(string.Format("{0}:{1}", Entities.systemuser, user.Id), user.GetStringField(userNameField));
                }

                var ownerTotals = new List <RecordCountByOwner>();
                foreach (var dictionary in recordCountsByUser)
                {
                    foreach (var owner in dictionary.Value)
                    {
                        if (owner.Key == noOwnerIndex)
                        {
                            ownerTotals.Add(new RecordCountByOwner(Service.GetDisplayName(dictionary.Key), owner.Value, noOwnerIndex, noOwnerIndex));
                        }
                        else
                        {
                            if (ownerKeyLabels.ContainsKey(owner.Key))
                            {
                                ownerTotals.Add(new RecordCountByOwner(Service.GetDisplayName(dictionary.Key), owner.Value, Service.GetDisplayName(owner.Key.Split(':')[0]), ownerKeyLabels[owner.Key]));
                            }
                            else
                            {
                                ownerTotals.Add(new RecordCountByOwner(Service.GetDisplayName(dictionary.Key), owner.Value, "Unknown", "Unknown"));
                            }
                        }
                    }
                }
                response.RecordCounts = ownerTotals;
            }
        }
예제 #4
0
        public override void ExecuteExtention(CustomisationImportRequest request, CustomisationImportResponse response,
                                              ServiceRequestController controller)
        {
            controller.LogLiteral("Reading excel spreadsheet");

            var readExcelResponse = ReadExcel(request, controller.Controller);

            response.AddResponseItems(readExcelResponse.ResponseItems);

            if (response.GetResponseItemsWithError().Any())
            {
                response.ExcelReadErrors = true;
                return;
            }

            CheckCrmConnection(controller.Controller);

            var createdRecordTypes = new List <string>();
            var createdFields      = new List <FieldMetadata>();

            if (request.Entities)
            {
                var importRecordsResponse = ImportRecordTypes(readExcelResponse.GetRecordMetadataToImport(), controller.Controller, response);
                createdRecordTypes.AddRange(importRecordsResponse.CreatedRecordTypes);
            }
            if (request.SharedOptionSets)
            {
                ImportSharedOptionSets(readExcelResponse.GetOptionSets(), controller.Controller, response);
            }
            if (request.Fields)
            {
                var importFieldsResponse = ImportFieldTypes(readExcelResponse.GetFieldMetadataToImport(), controller.Controller, response, createdRecordTypes);
                createdFields.AddRange(importFieldsResponse.CreatedFields);
            }
            if (request.FieldOptionSets)
            {
                ImportFieldOptionSets(readExcelResponse.GetFieldMetadataToImport().Values, controller.Controller, response, createdFields);
            }
            if (request.Views)
            {
                ImportViews(readExcelResponse.GetRecordMetadataToImport().Values, controller.Controller, response, createdRecordTypes);
            }
            if (request.Relationships)
            {
                ImportRelationships(readExcelResponse.GetRelationshipMetadataToImport(), controller.Controller, response);
            }

            if (request.AddToSolution)
            {
                var solutionId = request.Solution.Id;
                var solution   = RecordService.Get(Entities.solution, solutionId);
                var allImportedItemsMetadata = response.ImportedItems
                                               .Select(ri => ri.Metadata)
                                               .ToArray();
                //option sets
                Func <object, IMetadata> getMetadata = (object item) => RecordService.GetSharedPicklist(((PicklistOptionSet)item).SchemaName);
                var theseObjectsToAdd = allImportedItemsMetadata
                                        .Where(m => m is PicklistOptionSet)
                                        .Cast <PicklistOptionSet>()
                                        .Where(p => p.IsSharedOptionSet)
                                        .Cast <IMetadata>()
                                        .Distinct()
                                        .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.OptionSet, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //record types
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((RecordMetadata)item).SchemaName);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is RecordMetadata)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //fields
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((FieldMetadata)item).RecordType);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is FieldMetadata)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //relationships side 1
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((Many2ManyRelationshipMetadata)item).RecordType1);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is Many2ManyRelationshipMetadata)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //relationships side 2
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((Many2ManyRelationshipMetadata)item).RecordType2);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is Many2ManyRelationshipMetadata)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //field options
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((ImportPicklistFieldOptions)item).FieldMetadata.RecordType);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is ImportPicklistFieldOptions)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
                //views
                getMetadata       = (object item) => RecordService.GetRecordTypeMetadata(((ImportViews)item).SchemaName);
                theseObjectsToAdd = allImportedItemsMetadata
                                    .Where(m => m is ImportViews)
                                    .Cast <IMetadata>()
                                    .Distinct()
                                    .ToArray();
                AddComponentsToSolution(OptionSets.SolutionComponent.ObjectTypeCode.Entity, theseObjectsToAdd, getMetadata, response, solution, controller.Controller);
            }

            response.Message = response.HasResponseItemError
                ? "The Import Completed But The Following Errors Were Encountered During The Import"
                : "The Customisations Have Been Imported And Published";

            RecordService.ClearCache();
        }