예제 #1
0
        public void Execute(IServiceProvider serviceProvider)

        {
            String RelatedRecordLogicalName;

            tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)
                                              serviceProvider.GetService(typeof(IPluginExecutionContext));

            tracingService.Trace("OnPreValidateAssocoateOwnerTeam before assingning teams.");
            EntityReferenceCollection RelatedRecords = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];

            Relationship relationship = (Relationship)context.InputParameters["Relationship"];

            if (relationship.SchemaName != "teammembership_association")
            {
                return;
            }
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.MessageName == "Associate" || context.MessageName == "Disassociate")
            {
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is EntityReference)
                {
                    EntityReference entityReference = (EntityReference)context.InputParameters["Target"];

                    tracingService.Trace("contains target entity");
                    tracingService.Trace("logical name:" + entityReference.LogicalName);

                    if (entityReference.LogicalName != "team" && entityReference.LogicalName != "systemuser")
                    {
                        return;
                    }

                    OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(service);
                    //get team name
                    tracingService.Trace("after getting related records");

                    String  OwnerTeamName;
                    Boolean IsUserPartOfAnyOtherTeam = false;
                    int     index;
                    String  BusinessUnitName;
                    //process records only if its not a default team

                    if (context.MessageName == "Associate" && entityReference.LogicalName == "team")
                    {
                        Entity  TeamDetails        = service.Retrieve("team", entityReference.Id, new ColumnSet("name", "isdefault", "businessunitid"));
                        String  MemberShipTeamName = (String)TeamDetails["name"];
                        Boolean IsDefaultTeam      = (Boolean)TeamDetails["isdefault"];
                        if (MemberShipTeamName.EndsWith("CM-MEMBER"))
                        {
                            index = MemberShipTeamName.LastIndexOf("-");
                            if (index > 0)
                            {
                                OwnerTeamName = MemberShipTeamName.Substring(0, index) + "-OWNER";
                                tracingService.Trace("team found");
                                Guid?TeamId = GetTeamId(OwnerTeamName, orgSvcContext);
                                //Associate Team with User
                                tracingService.Trace("before handling membership team");
                                AddMembersToTeam(TeamId, RelatedRecords, service);
                                tracingService.Trace("after handling membership team");
                            }
                        }
                        {
                            tracingService.Trace("not a member team");
                        }
                    }
                    else if (context.MessageName == "Disassociate" && entityReference.LogicalName == "systemuser")
                    {
                        Guid UserId = entityReference.Id;

                        tracingService.Trace("getting system user details");
                        Entity UserDetails = service.Retrieve("systemuser", entityReference.Id, new ColumnSet("businessunitid"));
                        tracingService.Trace("related record type" + RelatedRecords.FirstOrDefault().LogicalName);
                        tracingService.Trace("getting team details");

                        Entity TeamDetails = service.Retrieve("team", RelatedRecords.FirstOrDefault().Id, new ColumnSet("name", "isdefault", "businessunitid"));
                        tracingService.Trace("after team details");

                        String MemberShipTeamName = (String)TeamDetails["name"];
                        if (MemberShipTeamName.EndsWith("CM-MEMBER"))
                        {
                            tracingService.Trace("its a member team");

                            //check if user is associated with any other team other than owner team
                            Guid   BusinessUnitId       = ((EntityReference)TeamDetails["businessunitid"]).Id;
                            String TeamBusinessUnitName = (String)service.Retrieve("businessunit", BusinessUnitId, new ColumnSet("name")).Attributes["name"];
                            Guid?  OwnerTeam            = GetTeamId(TeamBusinessUnitName + "-CM-OWNER", orgSvcContext);

                            //IsUserPartOfAnyOtherTeam = false;
                            IsUserPartOfAnyOtherTeam =
                                CheckIfUserIsMemberofAnyOtherTeam(TeamDetails.Id, OwnerTeam, UserId,
                                                                  ((EntityReference)TeamDetails["businessunitid"]).Id,
                                                                  orgSvcContext);
                            if (!IsUserPartOfAnyOtherTeam)
                            {
                                tracingService.Trace("before handling remove team member");
                                //if user is not part of any other team then remove that user

                                Guid[] Users = new Guid[1];
                                Users[0] = UserId;
                                RemoveMembersFromTeam(OwnerTeam, Users, service);
                                tracingService.Trace("after handling remove team member");
                            }
                        }

                        else
                        {
                            tracingService.Trace("not a meber team");
                        }
                    }

                    //}
                    //else //handle default team
                    //{
                    //    //CHEMS - UK - REACH - IT
                    //    MemberShipTeamName = (string)TeamDetails["name"] + "-CM-MEMBER";
                    //    tracingService.Trace("team found");
                    //    Guid? TeamId = GetTeamId(MemberShipTeamName, orgSvcContext);//GetMemberTeam.FirstOrDefault().teamid;
                    //                                                                //Associate Team with User
                    //    tracingService.Trace("before handling default team");
                    //    AddMembersToTeam(TeamId, RelatedRecords, service);
                    //    tracingService.Trace("after handling default team");

                    //}
                    tracingService.Trace("PreCreateAccountSetMultiSelectOptionSet after assinging values.");
                }
            }
        }