Suppose If your Activity is Task. We can Assign the task to specific users based on specific conditions by using workflow. Below I mentioned the screen shots.
Second way is you can Assign the activities to specific users by using plugin concept.. here I mentioned basic code
public class Distribution : IPlugin
{
private Guid _TaskID;
int count;
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target"))
{
if (context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "account")
{
try
{
//write something
}
Catch(exception e)
{
}
else
{
IOrganizationServiceFactory servicefactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);
_TaskID = entity.Id;
if (((OptionSetValue)entity.Attributes["subject"]).Value == “abc”)
{
AssignedTo("Undergraduate Nursing Program Team", entity, serviceProvider);
}
else if (((OptionSetValue)entity.Attributes["subject "]).Value == “dsf”)
{
AssignedTo("Masters Nursing Program Team", entity,serviceProvider);
}
public void AssignedTo(String Teamname, Entity entity, IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);
tracingService.Trace(Teamname + "entityname" + entity.LogicalName);
QueryExpression _queryforteams = new QueryExpression("team");
_queryforteams.ColumnSet = new ColumnSet(new string[] { "name", "businessunitid", "administratorid" });
EntityCollection _teams = client.RetrieveMultiple(_queryforteams);
foreach (var c_team in _teams.Entities)
{
tracingService.Trace(c_team.Attributes["name"].ToString());
if (c_team.Attributes["name"].ToString() == Teamname)
{
QueryExpression _Query = new QueryExpression();
_Query.EntityName = "systemuser";
_Query.ColumnSet = new ColumnSet(true);
_Query.ColumnSet.AddColumn("systemuserid");
_Query.LinkEntities.Add(new LinkEntity
{
LinkFromEntityName = "systemuser",
LinkToEntityName = "teammembership",
LinkFromAttributeName = "systemuserid",
LinkToAttributeName = "systemuserid",
LinkCriteria =
new FilterExpression
{
Conditions ={
new ConditionExpression("teamid",ConditionOperator.Equal,c_team.Id)
}
}
});
EntityCollection team_users = client.RetrieveMultiple(_Query);
tracingService.Trace(team_users.Entities.Count.ToString());
foreach (var tu in team_users.Entities)
{
QueryExpression qe = new QueryExpression("Task")
{
ColumnSet = new ColumnSet(new string[] { "Taskid", "ownerid", "subject" }),
Criteria =
{
Filters =
{
new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression("ownerid", ConditionOperator.Equal, tu.Id),
},
},
}
}
};
tracingService.Trace("Hi");
Guid contactId = ((EntityReference)c_team.Attributes["administratorid"]).Id;
AssignRequest assign = new AssignRequest
{
Assignee = new EntityReference("systemuser", contactId),
Target = new EntityReference("Task",
_TaskID)
};
client.Execute(assign);
}
}
}
}
}
}
}
}
}
}
I hope this will work for you. if it works Please Mark this answer as Verified.
Suppose If your Activity is Task. We can Assign the task to specific users based on specific conditions by using workflow. Below I mentioned the screen shots.