Skip to content

Instantly share code, notes, and snippets.

@furkankaracan
Created May 29, 2022 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furkankaracan/ca3f438056e9bdef4c99dc21e7be8584 to your computer and use it in GitHub Desktop.
Save furkankaracan/ca3f438056e9bdef4c99dc21e7be8584 to your computer and use it in GitHub Desktop.
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment