internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { //validate that the team template exists var ttId = (Guid)orgRequest["TeamTemplateId"]; var record = orgRequest["Record"] as EntityReference; var accessTeam = security.GetAccessTeam(ttId, record.Id); var membershiprow = security.GetTeamMembership(accessTeam.Id, (Guid)orgRequest["SystemUserId"]); db.Delete(membershiprow); if (membershiprow != null) { var poa = security.GetPOA((Guid)orgRequest["SystemUserId"], record.Id); if (poa != null) { //we need t update the poa record with the access masks from the the access teams the user is left in //get the users remaining team memberships var remainingAccessTeams = security.GetAccessTeams(record.Id); int mask = 0; foreach (var remainingAccessTeam in remainingAccessTeams) { var ttRow = core.GetEntity(new EntityReference("teamtemplate", remainingAccessTeam.GetAttributeValue <EntityReference>("teamtemplateid").Id)); var remainingTeamMembership = security.GetTeamMembership(remainingAccessTeam.Id, (Guid)orgRequest["SystemUserId"]); if (remainingTeamMembership != null) { mask = mask | ttRow.GetAttributeValue <int>("defaultaccessrightsmask"); } } security.OverwritePOAMask(poa.Id, mask); } } var resp = new RemoveUserFromRecordTeamResponse(); resp.Results["AccessTeamId"] = accessTeam.Id; return(resp); }
/// <summary> /// Executes the workflow activity. /// </summary> /// <param name="executionContext">The execution context.</param> protected override void Execute(CodeActivityContext executionContext) { // Create the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); if (tracingService == null) { throw new InvalidPluginExecutionException("Failed to retrieve tracing service."); } tracingService.Trace("Entered " + _processName + ".Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}", executionContext.ActivityInstanceId, executionContext.WorkflowInstanceId); // Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); if (context == null) { throw new InvalidPluginExecutionException("Failed to retrieve workflow context."); } tracingService.Trace(_processName + ".Execute(), Correlation Id: {0}, Initiating User: {1}", context.CorrelationId, context.InitiatingUserId); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { Guid connectionId = Connection.Get(executionContext).Id; Entity connection = service.Retrieve("connection", connectionId, new ColumnSet("record1id", "record2id")); EntityReference connectedFromId = (EntityReference)connection["record1id"]; EntityReference connectedToId = (EntityReference)connection["record2id"]; //only run this if the connection is to a user record if (connectedToId.LogicalName.ToUpper() == "SYSTEMUSER") { Guid teamTemplateId; string teamTemplateName = TeamTemplateName.Get(executionContext); //look up team template by name QueryByAttribute querybyexpression = new QueryByAttribute("teamtemplate"); querybyexpression.ColumnSet = new ColumnSet("teamtemplatename", "teamtemplateid"); querybyexpression.Attributes.AddRange("teamtemplatename"); querybyexpression.Values.AddRange(teamTemplateName); EntityCollection retrieved = service.RetrieveMultiple(querybyexpression); //if we find something, we're set if (retrieved.Entities.Count > 0) { teamTemplateId = retrieved.Entities[0].Id; } else { //throw exception if unable to find a matching template throw new Exception("could not find team template named: " + teamTemplateName); } RemoveUserFromRecordTeamRequest teamRemoveRequest = new RemoveUserFromRecordTeamRequest(); teamRemoveRequest.Record = connectedFromId; teamRemoveRequest.SystemUserId = connectedToId.Id; teamRemoveRequest.TeamTemplateId = teamTemplateId; RemoveUserFromRecordTeamResponse response = (RemoveUserFromRecordTeamResponse)service.Execute(teamRemoveRequest); } } catch (FaultException <OrganizationServiceFault> e) { tracingService.Trace("Exception: {0}", e.ToString()); // Handle the exception. throw; } catch (Exception e) { tracingService.Trace("Exception: {0}", e.ToString()); throw; } tracingService.Trace("Exiting " + _processName + ".Execute(), Correlation Id: {0}", context.CorrelationId); }