Add and Remove Users from Team Programmatically in Microsoft Dynamics CRM 2011

Posted: October 21, 2012 in Code Snippets, CRM 2011
            // Get the Guid of the team
            Guid teamId = new Guid("079EE428-F515-E211-B2D9-00155D025F00");
            
            // Guid of the CRM user1
            Guid user1Id = new Guid("486FA7B3-4A6F-E111-AA15-00155D025F09");

            // // Guid of the CRM user2
            Guid user2Id = new Guid("FB9CA21D-7F72-E111-AA15-00155D025F09");

            Guid[] members = new[] { user1Id, user2Id };

            // Method to add users to team
            AddMembersToTeam(teamId, members, service);

        public static void AddMembersToTeam(Guid teamId, Guid[] membersId, IOrganizationService service)
        {
            // Create the AddMembersTeamRequest object.
            AddMembersTeamRequest addRequest = new AddMembersTeamRequest();

            // Set the AddMembersTeamRequest TeamID property to the object ID of 
            // an existing team.
            addRequest.TeamId = teamId;

            // Set the AddMembersTeamRequest MemberIds property to an 
            // array of GUIDs that contains the object IDs of one or more system users.
            addRequest.MemberIds = membersId;

            // Execute the request.
            service.Execute(addRequest);
        }

        public static void RemoveMembersFromTeam(Guid teamId, Guid[] membersId, IOrganizationService service)
        {
            // Create the AddMembersTeamRequest object.
            RemoveMembersTeamRequest addRequest = new RemoveMembersTeamRequest();

            // Set the AddMembersTeamRequest TeamID property to the object ID of 
            // an existing team.
            addRequest.TeamId = teamId;

            // Set the AddMembersTeamRequest MemberIds property to an 
            // array of GUIDs that contains the object IDs of one or more system users.
            addRequest.MemberIds = membersId;

            // Execute the request.
            service.Execute(addRequest);
        }
Advertisements
Comments
  1. Pedro L. says:

    Good job man!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s