Sunday, 1 September 2013

Function to add members to Team Plugin Code



        //Function to Add members to Team
        private void addMember(IOrganizationService crmServ, List<string> userInfo, Guid TeamId)
        {
            #region add members to  team
            foreach (string memberId in userInfo)
            {
                if (!(string.IsNullOrWhiteSpace(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);
                    Guid userId = new Guid();
                    EntityCollection entColl = crmServ.RetrieveMultiple(queryExp);
                    foreach (Entity entity in entColl.Entities)
                    {
                        userId = (Guid)entity["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);
                    }
                }
            }
            #endregion
        }
    }
}