public void AddToTeam(Guid userId, Guid teamId)
 {
     if (!IsInTeam(userId, teamId))
     {
         var request = new AddMembersTeamRequest();
         request.TeamId    = teamId;
         request.MemberIds = new[] { userId };
         XrmService.Execute(request);
     }
 }
 public void RemoveFromTeam(Guid userId, Guid teamId)
 {
     if (IsInTeam(userId, teamId))
     {
         var request = new RemoveMembersTeamRequest();
         request.TeamId    = teamId;
         request.MemberIds = new[] { userId };
         XrmService.Execute(request);
     }
 }
Example #3
0
        /// <summary>
        /// If there is a separate user connection for the XrmService connection used for scripting limited security
        /// sets that connections user as a member of the teamId and removes them from any other team
        /// </summary>
        /// <param name="teamId">id of the team to set the test script user in</param>
        public void SetTestUserAsTeamMember(Guid teamId)
        {
            var testUserId  = XrmService.WhoAmI();
            var adminUserId = XrmServiceAdmin.WhoAmI();

            //if the same user for both the admin and standard connection then don't bother
            if (testUserId == adminUserId)
            {
                return;
            }

            //get the non-default teams th4e test user a member of
            var teamQuery = XrmServiceAdmin.BuildQuery(Entities.team,
                                                       fields: new string[0],
                                                       conditions: new[] { new ConditionExpression(Fields.team_.isdefault, ConditionOperator.NotEqual, true) });
            var memberJoin = teamQuery.AddLink(Relationships.team_.teammembership_association.EntityName, Fields.team_.teamid, Fields.team_.teamid);

            memberJoin.LinkCriteria.AddCondition(new ConditionExpression(Fields.systemuser_.systemuserid, ConditionOperator.Equal, testUserId));
            var teamsMemberships = XrmServiceAdmin.RetrieveAll(teamQuery);

            //if they are already only a member of the team then return
            if (teamsMemberships.Count() == 1 && teamsMemberships.First().Id == teamId)
            {
                return;
            }

            //remove them from the teams they are a member of
            foreach (var team in teamsMemberships)
            {
                if (!team.GetBoolean(Fields.team_.isdefault))
                {
                    var removeRequest = new RemoveMembersTeamRequest();
                    removeRequest.TeamId    = team.Id;
                    removeRequest.MemberIds = new[] { testUserId };
                    XrmService.Execute(removeRequest);
                }
            }

            //add them to the team
            var addRequest = new AddMembersTeamRequest();

            addRequest.TeamId    = teamId;
            addRequest.MemberIds = new[] { testUserId };
            XrmService.Execute(addRequest);
        }