Sunday, 1 September 2013

Associated member to a team Plugin Code



        // Function to Associate Membersto team
        private string AssociateMembersToTeam(IOrganizationService crmServ, string memberId, Guid teamId)
        {
            #region add members to  Team
            string MemberId = "";
            QueryExpression queryExp = new QueryExpression();
            queryExp.EntityName = "systemuser";
            queryExp.ColumnSet = new ColumnSet();
            queryExp.ColumnSet.Columns.Add("systemuserid");
            ConditionExpression conExp = new ConditionExpression();
            conExp.AttributeName = "domainname";
            conExp.Operator = ConditionOperator.Equal;
            conExp.Values.Add(memberId);
            ConditionExpression userStatus = new ConditionExpression();
            userStatus.AttributeName = "isdisabled";
            userStatus.Operator = ConditionOperator.Equal;
            userStatus.Values.Add(false);
            FilterExpression fep = new FilterExpression();
            fep.FilterOperator = LogicalOperator.And;
            fep.Conditions.Add(conExp);
            fep.Conditions.Add(userStatus);
            queryExp.Criteria.AddFilter(fep);


            EntityCollection entCollection = crmServ.RetrieveMultiple(queryExp);

            {

                foreach (Entity user in entCollection.Entities)
                {
                    MemberId = memberId + ",";
                    Guid userId = (Guid)user["systemuserid"];
                    AddMembersTeamRequest addMembereTeamRequest = new AddMembersTeamRequest();
                    addMembereTeamRequest.TeamId = teamId;
                    Guid[] arrayMembers = new Guid[1];
                    arrayMembers[0] = userId;
                    addMembereTeamRequest.MemberIds = arrayMembers;
                    AddMembersTeamResponse addMembersTeamresp = (AddMembersTeamResponse)crmServ.Execute(addMembereTeamRequest);
                }
            }
            return MemberId;
            #endregion
        }
    }
}