public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { AddUserToRecordTeamRequest addReq = (AddUserToRecordTeamRequest)request; EntityReference target = addReq.Record; Guid systemuserId = addReq.SystemUserId; Guid teamTemplateId = addReq.TeamTemplateId; if (target == null) { throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team without target"); } if (systemuserId == Guid.Empty) { throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team without user"); } if (teamTemplateId == Guid.Empty) { throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team without team"); } IOrganizationService service = ctx.GetOrganizationService(); AddUserToRecordTeamRequest addRequest = new AddUserToRecordTeamRequest { SystemUserId = systemuserId, Record = target, TeamTemplateId = teamTemplateId }; AddUserToRecordTeamResponse response = (AddUserToRecordTeamResponse)service.Execute(addRequest); return(response); }
internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { var request = MakeRequest <AddUserToRecordTeamRequest>(orgRequest); var resp = new AddUserToRecordTeamResponse(); var settings = MockupExecutionContext.GetSettings(request); //check to see if the access team exists //where regarding object id = request.record.id //and teamtemplateid = request.teamtemplated var currentTeams = db.GetDBEntityRows("team"); currentTeams = currentTeams.Where(x => x.GetColumn("regardingobjectid") != null); currentTeams = currentTeams.Where(x => (x.ToEntity().GetAttributeValue <EntityReference>("regardingobjectid").Id == request.Record.Id)); currentTeams = currentTeams.Where(x => x.GetColumn("teamtemplateid") != null); currentTeams = currentTeams.Where(x => (x.ToEntity().GetAttributeValue <EntityReference>("teamtemplateid").Id == request.TeamTemplateId)); Entity team; if (!currentTeams.Any()) { team = new Entity("team"); team.Id = Guid.NewGuid(); team["regardingobjectid"] = request.Record; team["teamtemplateid"] = new EntityReference("teamtemplate", request.TeamTemplateId); team["teamtype"] = new OptionSetValue(1); //access team db.Add(team); resp.Results["AccessTeamId"] = team.Id; } else { team = currentTeams.Single().ToEntity(); } //and check to see if the user is a member of it var currentTeamMembers = db.GetDBEntityRows("teammembership"); currentTeamMembers = currentTeamMembers.Where(x => x.GetColumn("teamid") != null); currentTeamMembers = currentTeamMembers.Where(x => (x.ToEntity().GetAttributeValue <Guid>("teamid") == team.Id)); currentTeamMembers = currentTeamMembers.Where(x => x.GetColumn("systemuserid") != null); currentTeamMembers = currentTeamMembers.Where(x => (x.ToEntity().GetAttributeValue <Guid>("systemuserid") == request.SystemUserId)); if (!currentTeamMembers.Any()) { var teamMember = new Entity("teammembership"); teamMember["teamid"] = team.Id; teamMember["systemuserid"] = request.SystemUserId; db.Add(teamMember); } return(resp); }
internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { //validate that the team template exists var ttId = (Guid)orgRequest["TeamTemplateId"]; var ttRow = core.GetEntity(new EntityReference("teamtemplate", ttId)); var record = orgRequest["Record"] as EntityReference; var accessTeam = security.GetAccessTeam(ttId, record.Id); if (accessTeam == null) { accessTeam = security.AddAccessTeam(ttId, record); } var membershiprow = security.GetTeamMembership(accessTeam.Id, (Guid)orgRequest["SystemUserId"]); if (membershiprow == null) { membershiprow = security.AddTeamMembership(accessTeam.Id, (Guid)orgRequest["SystemUserId"]); var poa = security.GetPOA((Guid)orgRequest["SystemUserId"], record.Id); if (poa == null) { poa = security.AddPOA((Guid)orgRequest["SystemUserId"], record, ttRow.GetAttributeValue <int>("defaultaccessrightsmask")); } else { //update the existing poa to include the new privlege security.UpdatePOAMask(poa.Id, ttRow.GetAttributeValue <int>("defaultaccessrightsmask")); } } var resp = new AddUserToRecordTeamResponse(); 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); } AddUserToRecordTeamRequest teamAddRequest = new AddUserToRecordTeamRequest(); teamAddRequest.Record = connectedFromId; teamAddRequest.SystemUserId = connectedToId.Id; teamAddRequest.TeamTemplateId = teamTemplateId; AddUserToRecordTeamResponse response = (AddUserToRecordTeamResponse)service.Execute(teamAddRequest); } } 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); }