public static void RefreshXrmServices(IXrmRecordConfiguration xrmConfiguration, IApplicationController controller, XrmRecordService xrmRecordService = null) { controller.RegisterInstance <IXrmRecordConfiguration>(xrmConfiguration); var serviceFactory = controller.ResolveType <IOrganizationConnectionFactory>(); xrmRecordService = xrmRecordService ?? new XrmRecordService(xrmConfiguration, serviceFactory, formService: new XrmFormService()); xrmRecordService.XrmRecordConfiguration = xrmConfiguration; controller.RegisterInstance(xrmRecordService); LastXrmConfiguration = xrmConfiguration; var spawnConnectAsynch = !xrmConfiguration.UseXrmToolingConnector; if (xrmConfiguration.OrganizationUniqueName == null && !xrmConfiguration.UseXrmToolingConnector) { RefreshConnectionNotification(controller, "No Active Connection"); } else if (!spawnConnectAsynch) { RefreshConnectionNotification(controller, string.Format("Connected To '{0}'", xrmConfiguration)); } else if (controller.RunThreadsAsynch && spawnConnectAsynch) { controller.DoOnAsyncThread(() => { try { RefreshConnectionNotification(controller, $"Connecting To '{xrmConfiguration}'", isLoading: true); var verify = xrmRecordService.VerifyConnection(); if (LastXrmConfiguration != xrmConfiguration) { return; } if (verify.IsValid) { RefreshConnectionNotification(controller, string.Format("Connected To '{0}'", xrmConfiguration)); var preLoadRecordTypes = xrmRecordService.GetAllRecordTypes(); } else { RefreshConnectionNotification(controller, string.Format("Error Connecting To '{0}'", xrmConfiguration)); } } catch (Exception ex) { if (LastXrmConfiguration != xrmConfiguration) { return; } RefreshConnectionNotification(controller, ex.Message); controller.ThrowException(ex); } }); } }
private void LoadComponentItems(IEnumerable <string> ids, XrmRecordService xrmRecordService) { if (ComponentTypeKey == OptionSets.SolutionComponent.ObjectTypeCode.Entity) { AllItems = xrmRecordService .GetAllRecordTypes() .Select(r => xrmRecordService.GetRecordTypeMetadata(r)) .Where(m => ids.Contains(m.MetadataId)) .Select(m => new AddToSolutionComponentItem(m.MetadataId, m.DisplayName)) .ToArray(); } else if (ComponentTypeKey == OptionSets.SolutionComponent.ObjectTypeCode.OptionSet) { AllItems = xrmRecordService .GetSharedPicklists() .Where(m => ids.Contains(m.MetadataId)) .Select(m => new AddToSolutionComponentItem(m.MetadataId, m.DisplayName)) .ToArray(); } else { var propTypeMaps = new Dictionary <int, string>(); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.SystemForm, Entities.systemform); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.EmailTemplate, Entities.template); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.PluginAssembly, Entities.pluginassembly); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.SDKMessageProcessingStep, Entities.sdkmessageprocessingstep); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.Report, Entities.report); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.Role, Entities.role); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.WebResource, Entities.webresource); propTypeMaps.Add(OptionSets.SolutionComponent.ObjectTypeCode.Workflow, Entities.workflow); if (!propTypeMaps.ContainsKey(ComponentTypeKey)) { throw new NotImplementedException($"Component Type {ComponentTypeKey} Is Not Implemented"); } var recordType = propTypeMaps[ComponentTypeKey]; var primaryKeyField = xrmRecordService.GetPrimaryKey(recordType); var nameField = xrmRecordService.GetPrimaryField(recordType); AllItems = xrmRecordService .RetrieveAllOrClauses(recordType, ids.Select(i => new Condition(primaryKeyField, ConditionType.Equal, i))) .Select(e => new AddToSolutionComponentItem(e.Id, e.GetStringField(nameField))) .OrderBy(c => c.Name) .ToArray(); } ItemsSelection = AllItems .Select(i => new SelectableAddToSolutionComponentItem(i.Id, i.Name)) .ToArray(); }
public static void RefreshXrmServices(IXrmRecordConfiguration xrmConfiguration, IApplicationController controller) { controller.RegisterInstance <IXrmRecordConfiguration>(xrmConfiguration); var serviceConnection = new XrmRecordService(xrmConfiguration, controller.ResolveType <LogController>(), formService: new XrmFormService()); controller.RegisterInstance(serviceConnection); LastXrmConfiguration = xrmConfiguration; if (xrmConfiguration.OrganizationUniqueName == null) { controller.AddNotification("XRMCONNECTION", "No Active Connection"); } else if (controller.RunThreadsAsynch) { controller.DoOnAsyncThread(() => { try { controller.AddNotification("XRMCONNECTION", $"Connecting To '{xrmConfiguration}'", isLoading: true); var verify = serviceConnection.VerifyConnection(); if (LastXrmConfiguration != xrmConfiguration) { return; } if (verify.IsValid) { controller.AddNotification("XRMCONNECTION", string.Format("Connected To '{0}'", xrmConfiguration)); var preLoadRecordTypes = serviceConnection.GetAllRecordTypes(); } else { controller.AddNotification("XRMCONNECTION", string.Format("Error Connecting To '{0}'", xrmConfiguration)); } } catch (Exception ex) { if (LastXrmConfiguration != xrmConfiguration) { return; } controller.AddNotification("XRMCONNECTION", ex.Message); controller.ThrowException(ex); } }); } }
public override void ExecuteExtention(DeployIntoFieldRequest request, DeployIntoFieldResponse response, ServiceRequestController controller) { var records = new List <IRecord>(); var publishIds = new List <string>(); var numberToDo = request.Files.Count(); var numberDone = 0; foreach (var file in request.Files.Select(f => f?.Replace("∕", "∕"))) { var fileInfo = new FileInfo(file); controller.UpdateProgress(++numberDone, numberToDo, "Importing " + fileInfo.Name); var thisResponseItem = new DeployIntoFieldResponseItem() { Name = fileInfo.Name }; response.AddResponseItem(thisResponseItem); try { var containingFolderName = fileInfo.Directory.Name; var containingFolderParentName = fileInfo.Directory.Parent != null ? fileInfo.Directory.Parent.Name : null; //get target record type string recordType = null; if (Service.GetAllRecordTypes().Any(r => r == containingFolderName)) { recordType = Service.GetAllRecordTypes().First(r => r == containingFolderName); } else if (Service.GetAllRecordTypes().Any(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower())) { recordType = Service.GetAllRecordTypes().First(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower()); } else { throw new NullReferenceException($"Could not find matching type by logical or display name for folder name of {containingFolderName}"); } thisResponseItem.RecordType = recordType; if (recordType == Entities.adx_webfile) { //this one goes into an attachment var matchingRecord = GetRecordToDeployInto(recordType, fileInfo.Name, containingFolderParentName); if (matchingRecord == null) { throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into"); } thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open"); //get matching attachment by name else create a new one var fileAttachments = Service.RetrieveAllAndClauses(Entities.annotation, new[] { new Condition(Fields.annotation_.filename, ConditionType.Equal, fileInfo.Name), new Condition(Fields.annotation_.objectid, ConditionType.Equal, matchingRecord.Id) }).OrderBy(n => n.GetDateTime(Fields.annotation_.createdon)).ToArray(); var contentBytes = File.ReadAllBytes(file); var contentBase64String = Convert.ToBase64String(contentBytes); if (fileAttachments.Any()) { //lets update the last modifed one var attachmentToUpdate = fileAttachments.First(); if (attachmentToUpdate.GetStringField(Fields.annotation_.documentbody) != contentBase64String) { attachmentToUpdate.SetField(Fields.annotation_.documentbody, contentBase64String, Service); Service.Update(attachmentToUpdate, new[] { Fields.annotation_.documentbody }); thisResponseItem.Updated = true; } } else { //lets create a new attachment var newAttachment = Service.NewRecord(Entities.annotation); newAttachment.SetLookup(Fields.annotation_.objectid, matchingRecord.Id, matchingRecord.Type); newAttachment.SetField(Fields.annotation_.subject, fileInfo.Name, Service); newAttachment.SetField(Fields.annotation_.filename, fileInfo.Name, Service); newAttachment.SetField(Fields.annotation_.documentbody, contentBase64String, Service); Service.Create(newAttachment); thisResponseItem.Created = true; } } else { //get the record with the same name as the file var nameToMatch = fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf(".")); var matchingRecord = GetRecordToDeployInto(recordType, nameToMatch, containingFolderParentName); if (matchingRecord == null) { throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into"); } thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open"); var targetField = GetTargetField(fileInfo, recordType); var contentText = File.ReadAllText(file); if (matchingRecord.GetStringField(targetField) != contentText) { matchingRecord.SetField(targetField, contentText, Service); Service.Update(matchingRecord, new[] { targetField }); thisResponseItem.Updated = true; } } } catch (Exception ex) { thisResponseItem.Exception = ex; } } }
public void VsixAddPortalCodeTest() { RecreatePortalData(createSecondDuplicateSite: true); var app = CreateAndLoadTestApplication <AddPortalCodeModule>(); var fakeProjectName = "FakeProjectName"; var directoryInfo = Directory.CreateDirectory(Path.Combine(VisualStudioService.SolutionDirectory, fakeProjectName)); VisualStudioService.SetSelectedItem(new FakeVisualStudioSolutionFolder(directoryInfo.FullName)); var websiteName = "Fake Site 1"; var webSite = XrmRecordService.GetFirst(Entities.adx_website, Fields.adx_website_.adx_name, websiteName); var request = new AddPortalCodeRequest { WebSite = XrmRecordService.ToLookup(webSite), ExportWhereFieldEmpty = true, CreateFolderForWebsiteName = true }; var dialog = app.NavigateToDialog <AddPortalCodeModule, AddPortalCodeDialog>(); var entryForm = app.GetSubObjectEntryViewModel(dialog); entryForm.GetLookupFieldFieldViewModel(nameof(AddPortalCodeRequest.WebSite)).SetValue(entryForm.GetLookupFieldFieldViewModel(nameof(AddPortalCodeRequest.WebSite)).ItemsSourceAsync.First(r => r.Record != null).Record); entryForm.GetBooleanFieldFieldViewModel(nameof(AddPortalCodeRequest.ExportWhereFieldEmpty)).Value = true; entryForm.GetBooleanFieldFieldViewModel(nameof(AddPortalCodeRequest.CreateFolderForWebsiteName)).Value = true; var section = entryForm.GetFieldSection(AddPortalCodeRequest.Sections.RecordsToInclude); var func = section.CustomFunctions.First(c => c.Id == "SELECTALL"); func.Invoke(); Assert.IsTrue(entryForm.GetEnumerableFieldViewModel(nameof(AddPortalCodeRequest.RecordsToExport)).GridRecords .All(r => r.GetBooleanFieldFieldViewModel(nameof(AddPortalCodeRequest.PortalRecordsToExport.Selected)).Value ?? false)); var webTemplateRow = entryForm.GetEnumerableFieldViewModel(nameof(AddPortalCodeRequest.RecordsToExport)) .GridRecords .First(gr => gr.GetRecordTypeFieldViewModel(nameof(AddPortalCodeRequest.PortalRecordsToExport.RecordType)).Value.Key == Entities.adx_webtemplate); webTemplateRow.GetBooleanFieldFieldViewModel(nameof(AddPortalCodeRequest.PortalRecordsToExport.IncludeAll)).Value = false; //this now auto runs when the flag above iunchecked //webTemplateRow.GetEnumerableFieldViewModel(nameof(AddPortalCodeRequest.PortalRecordsToExport.RecordsToInclude)).BulkAddButton.Invoke(); var templateRecordSelectionForm = entryForm.ChildForms.First() as MultiSelectDialogViewModel <PicklistOption>; templateRecordSelectionForm.ItemsSource.First().Select = true; templateRecordSelectionForm.ItemsSource.Last().Select = true; templateRecordSelectionForm.ApplyButtonViewModel.Invoke(); Assert.IsFalse(entryForm.ChildForms.Any()); Assert.IsTrue(entryForm.Validate()); entryForm.SaveButtonViewModel.Invoke(); var responseViewModel = app.GetCompletionViewModel(dialog); var response = responseViewModel.GetObject() as AddPortalCodeResponse; Assert.IsFalse(response.HasError); var rootFolder = Path.Combine(directoryInfo.FullName, websiteName); foreach (var typesFolder in new DirectoryInfo(rootFolder).GetDirectories()) { var fileCountInDirectory = typesFolder.GetFiles().Count(); var typeLabel = typesFolder.Name; var type = XrmRecordService.GetAllRecordTypes().First(rt => XrmRecordService.GetDisplayName(rt) == typeLabel); var recordsOfType = XrmRecordService.RetrieveAll(type, null); if (type == Entities.adx_webpage) { //web pages have a parent and child where onluy the ch9ild is exported // (multi language not implemented) // + each has html, css & javascript Assert.AreEqual((recordsOfType.Count() / 4) * 3, fileCountInDirectory); } else if (type == Entities.adx_webtemplate) { Assert.AreEqual(2, fileCountInDirectory); } else { Assert.AreEqual(recordsOfType.Count() / 2, fileCountInDirectory); } } }