Ejemplo n.º 1
0
        /// <summary>
        /// Run the sample.
        /// </summary>
        /// <param name="serverConfig">configuration for the server.</param>
        /// <param name="promptToDelete">
        /// whether or not to prompt the user to delete created records.
        /// </param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptToDelete)
        {
            using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
            {
                using (_context = new AdventureWorksCycleServiceContext(_serviceProxy))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // This statments checks whether Standard Email templates are present
                    var emailTemplateId = (
                        from emailTemplate in _context.TemplateSet
                        where emailTemplate.Title == "Contact Reconnect"
                        select emailTemplate.Id
                        ).FirstOrDefault();

                    if (emailTemplateId != Guid.Empty)
                    {
                        CreateRequiredRecords();

                        // Perform the bulk delete.  If you want to perform a recurring delete
                        // operation, then leave this as it is.  Otherwise, pass in false as the
                        // first parameter.
                        PerformBulkDelete(true, promptToDelete);
                    }
                    else
                    {
                        throw new ArgumentException("Standard Email Templates are missing");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method first creates 3 users, a team, 4 leads and a business unit.
        /// It assigns two users to the team, and gives each user and the team a lead.
        /// Then it reassigns all the leads from one of the users to another user using
        /// the ReassignObjectsSystemUserRequest. Next, it reassigns all the leads from
        /// the team to a user using the ReassignObjectsOwnerRequest. Third, it reassigns
        /// one user from the root business unit to the created business unit, using the
        /// SetBusinessSystemUserRequest message. Fourth, it reassigns all users from
        /// the created business unit to the root business unit and deletes the created
        /// business unit. Finally, it removes all users from the created team and
        /// deletes the team.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>

        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
//<snippetReassignBusinessUnitMembers1>
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly
                // disposed.
                using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                    // Using the ServiceContext class provides access to the LINQ provider
                    using (_context = new AdventureWorksCycleServiceContext(_serviceProxy))
                    {
                        // This statement is required to enable early-bound type support.
                        _serviceProxy.EnableProxyTypes();

                        CreateRequiredRecords();

                        Console.WriteLine();
                        PrintLeads();

                        var users = from user in _context.SystemUserSet
                                    select new { user.FullName, user.Id };
                        Dictionary <Guid, String> userMapping = new Dictionary <Guid, String>();
                        foreach (var user in users)
                        {
                            userMapping.Add(user.Id, user.FullName);
                        }

                        #region ReassignObjectsSystemUserRequest

                        // create the request
                        ReassignObjectsSystemUserRequest reassignRequest =
                            new ReassignObjectsSystemUserRequest()
                        {
                            ReassignPrincipal =
                                new EntityReference(SystemUser.EntityLogicalName, _users[1]),
                            UserId = _users[2]
                        };

                        // execute the request
                        Console.WriteLine();
                        Console.WriteLine(
                            "  Reassigning leads from {0} to {1}",
                            userMapping[_users[2]],
                            userMapping[_users[1]]);
                        _serviceProxy.Execute(reassignRequest);

                        // check results
                        PrintLeads();

                        #endregion

                        #region ReassignObjectsOwnerRequest

                        // create the request
                        ReassignObjectsOwnerRequest request =
                            new ReassignObjectsOwnerRequest()
                        {
                            FromPrincipal = _team.ToEntityReference(),
                            ToPrincipal   =
                                new EntityReference(SystemUser.EntityLogicalName, _users[0])
                        };

                        // execute the request
                        Console.WriteLine();
                        Console.WriteLine(
                            "  Reassigning leads from {0} to {1}",
                            _team.Name, userMapping[_users[0]]);
                        _serviceProxy.Execute(request);

                        // check results
                        PrintLeads();

                        #endregion

                        #region reassign business unit members

                        Console.WriteLine();
                        Console.WriteLine("  Adding a user to the created business unit");
                        // track what permissions the user had before reassigning to the new
                        // business unit so that the permissions can be restored when the
                        // user is assigned back to the business unit
                        _originalRolesIds = new List <Guid>();
                        var roleIds = from user in _context.SystemUserSet
                                      join systemuserrole in _context.SystemUserRolesSet
                                      on user.SystemUserId equals systemuserrole.SystemUserId
                                      join role in _context.RoleSet
                                      on systemuserrole.RoleId equals role.RoleId
                                      where user.SystemUserId.Value == _users[2]
                                      select role.RoleId.Value;

                        foreach (var roleId in roleIds)
                        {
                            _originalRolesIds.Add(roleId);
                        }

                        // add user to the created business unit
                        _serviceProxy.Execute(new SetBusinessSystemUserRequest()
                        {
                            BusinessId        = _buisnessUnit.Id,
                            ReassignPrincipal = new EntityReference(
                                SystemUser.EntityLogicalName,
                                _users[2]),
                            UserId = _users[2]
                        });

                        #endregion

                        #region delete business unit

                        Console.WriteLine();
                        Console.WriteLine("  Deleting created business unit");

                        // remove all users from the business unit, moving them back to the
                        // parent business unit
                        _serviceProxy.Execute(new SetBusinessSystemUserRequest()
                        {
                            BusinessId        = _rootBusinessUnit.Id,
                            ReassignPrincipal = new EntityReference(
                                SystemUser.EntityLogicalName, _users[2]),
                            UserId = _users[2]
                        });

                        // give the user back their original security roles
                        foreach (var roleId in roleIds)
                        {
                            _serviceProxy.Associate(
                                SystemUser.EntityLogicalName,
                                _users[2],
                                new Relationship("systemuserroles_association"),
                                new EntityReferenceCollection()
                            {
                                new EntityReference(
                                    Role.EntityLogicalName,
                                    roleId
                                    )
                            }
                                );
                        }

                        // deactivate business unit before deleting it
                        _serviceProxy.Execute(new SetStateRequest()
                        {
                            EntityMoniker = _buisnessUnit.ToEntityReference(),
                            // mark the state as inactive (value 1)
                            State  = new OptionSetValue(1),
                            Status = new OptionSetValue(-1)
                        });

                        // delete business unit
                        _serviceProxy.Delete(BusinessUnit.EntityLogicalName,
                                             _buisnessUnit.Id);

                        #endregion

                        #region remove users from team

                        var teamMembers = from team in _context.TeamSet
                                          join membership in _context.TeamMembershipSet
                                          on team.TeamId equals membership.TeamId
                                          where team.TeamId == _team.Id
                                          select membership.SystemUserId.Value;

                        _serviceProxy.Execute(new RemoveMembersTeamRequest()
                        {
                            MemberIds = teamMembers.ToArray(),
                            TeamId    = _team.Id
                        });

                        #endregion

                        #region delete team

                        Console.WriteLine();
                        Console.WriteLine("  Deleting the team");

                        // Delete the team
                        _serviceProxy.Delete(Team.EntityLogicalName, _team.Id);

                        #endregion

                        DeleteRequiredRecords(promptforDelete);
                    }
                //</snippetReassignBusinessUnitMembers1>
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }