static void Main(string[] args) { //IOrganizationService service = XrmConnection.Connect(connectionDetails); XrmConnection connection = new XrmConnection(); Console.WriteLine("Connected to CRM. Press enter to continue..."); Console.ReadLine(); RoleManager rm = new RoleManager(connection.service, connection.caller); Console.Write("User (Guid) to test:"); //Guid userToTestGuid = new Guid(Console.ReadLine()); Guid userToTestGuid = new Guid("1065A1BE-6BA3-E611-A161-000C29C78F2E"); EntityReference testUser = new EntityReference("systemuser", userToTestGuid); Console.Write("User (Guid) to copy from:"); //Guid userToCopyGuid = new Guid(Console.ReadLine()); Guid userToCopyGuid = new Guid("84429B63-C273-E611-A14E-EACD0C285D01"); EntityReference copyFromUser = new EntityReference("systemuser", userToCopyGuid); EntityCollection teams = rm.RetrieveUserTeams(copyFromUser); EntityCollection roles = rm.RetrieveUserRoles(copyFromUser); List <Entity> rolesList = new List<Entity>(roles.Entities); List<Entity> teamsList = new List<Entity>(teams.Entities); rm.AssignBusinessUnit(testUser, copyFromUser); rm.AddRolesToPrincipal(rolesList, testUser, rolesList); }
private async void ConnectButton_Click(object sender, RoutedEventArgs e) { CancelButton.IsEnabled = ConnectButton.IsEnabled = false; var progress = new ProgressIndicatorHost(Dispatcher, 3, true); try { string whoami = null; var connectionData = ((ViewModels.ConnectionDialogViewModel)DataContext).ConnectionData; // Attempt connect (non-blocking) Exception failException = null; await Task.Factory.StartNew(() => { using (var connection = new XrmConnection()) { try { progress.SetStatus(1, "Connecting to CRM-server..."); connection.Connect(connectionData); progress.SetStatus(2, "Getting WhoAmI..."); whoami = connection.WhoAmI(); progress.SetStatus(3, "Done!"); } catch (Exception exception) { failException = exception; } } }); if (failException != null) { MessageBox.Show("Error connecting to CRM-server:\r\n" + failException.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; return; } // if (whoami == null) { whoami = "Something went wrong. Unable to retrieve WhoAmI fullname. We'll try to pretend everything is ok."; } // Display result MessageBox.Show(whoami); // Done DialogResult = true; } finally { CancelButton.IsEnabled = ConnectButton.IsEnabled = true; progress.Dispose(); } }
public Entity GetTestSettings() { var settings = XrmService.GetFirst("jmcg_wstestsettings"); if (settings == null) { settings = new Entity("jmcg_wstestsettings"); var connection = new XrmConnection(XrmConfiguration); settings.SetField("jmcg_name", "Test Workflow Settings"); settings.SetField("jmcg_crminstanceurl", connection.GetWebUrl()); settings = CreateAndRetrieve(settings); } return(settings); }
protected void OnItemSaving(object sender, EntityFormSavingEventArgs e) { XrmConnection _conn = new XrmConnection(); var service = new XrmServiceContext(_conn); Guid entityId = EntityForm1.EntitySourceDefinition.ID; string logicalName = EntityForm1.EntitySourceDefinition.LogicalName; e.Cancel = service.HasDuplicate(logicalName, e.Values, entityId); if (e.Cancel) { Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "DupeDetectedNotification();", true); } }
private BranchSettingsViewModel UserBranchConfiguration(string username) { XrmConnection _conn = new XrmConnection(); var service = new Site.Areas.DMS_Api.XrmWebService.UserManager(_conn); BranchSettingsViewModel userBranchVM = service.GetUserBranch(username); if (ConfigurationManager.ConnectionStrings[userBranchVM.BranchName] == null) { userBranchVM.IsBranchConfigured = false; return(userBranchVM); } userBranchVM.IsBranchConfigured = true; return(userBranchVM); }