Whether you are Developer, Analyst or Consultant, there may be requirements for to add multiple users to a team. For instance, the company has created a couple of new teams in its organization chart and want you to implement this new teams to Dynamics 365.
Do you prefer navigating through 20 User records by hand to adding them to a specific team and spend a half hour or do you prefer using a workflow to complete this process in just a minute ?😀
Here are the steps to achieve this:
- Use the below code to create a Custom Workflow activity and deploy it.
- Create an on-demand workflow that works on SystemUser entity.
- Add this Workflow Activity as your only step.
- Select Input property Team with the Team record that you want to add user.
- Select your User records via Advanced find or User Entity view.
- Run the on-demand workflow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Crm.Sdk.Messages; | |
using Microsoft.Xrm.Sdk; | |
using Microsoft.Xrm.Sdk.Workflow; | |
using System; | |
using System.Activities; | |
namespace D365_Core_Workflows.WorkflowActivities | |
{ | |
public class AddSelectedUserToTeam : CodeActivity | |
{ | |
[RequiredArgument] | |
[Input("Team")] | |
[ReferenceTarget("team")] | |
public InArgument<EntityReference> Team { get; set; } | |
#region Service Parameters | |
private ITracingService tracingService; | |
private IWorkflowContext context; | |
private IOrganizationServiceFactory serviceFactory; | |
private IOrganizationService service; | |
#endregion Service Parameters | |
protected override void Execute(CodeActivityContext executionContext) | |
{ | |
#region Initializing Services | |
try | |
{ | |
tracingService = executionContext.GetExtension<ITracingService>(); | |
context = executionContext.GetExtension<IWorkflowContext>(); | |
serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); | |
service = serviceFactory.CreateOrganizationService(context.UserId); | |
tracingService.Trace("Services are initialized."); | |
} | |
catch (Exception e) | |
{ | |
throw new InvalidOperationException($"There was an error during initializing the services: {e.Message}"); | |
} | |
#endregion Initializing Services | |
#region Parameters | |
tracingService.Trace("Reading Input Parameters"); | |
var userRef = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId); | |
var teamRef = this.Team.Get(executionContext); | |
if (userRef.Id == Guid.Empty || teamRef.Id == Guid.Empty) | |
throw new InvalidPluginExecutionException("Invalid input parameters! Please contact with your System Administrator."); | |
#endregion Parameters | |
#region Add User To Team | |
tracingService.Trace("Starting AddMembersTeamRequest"); | |
AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest() | |
{ | |
MemberIds = new[] { userRef.Id }, | |
TeamId = teamRef.Id | |
}; | |
_ = (AddMembersTeamResponse)service.Execute(addMembersTeamRequest); | |
tracingService.Trace("Ending AddMembersTeamRequest"); | |
#endregion Add User To Team | |
} | |
} | |
} |
Advertisement
One thought on “Dynamics 365 Workflow Activity- Add Selected Users To a Team as a Bulk(AddMembersTeamRequest)”