I have implemented your needs with the plugin.
I use the Plugin Registration Tool to register plugins.
public class PreventRemoveFromQueue : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;
IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
if (context.InputParameters.ContainsKey("QueueItemId"))
{
IOrganizationServiceFactory serviceFactory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Guid queueItemId = (Guid)context.InputParameters["QueueItemId"];
ColumnSet columns = new ColumnSet();
columns.AllColumns = true;
Entity queueItem = service.Retrieve("queueitem", queueItemId, columns);
if (!queueItem.Attributes.Contains("workerid"))
{
return;
}
EntityReference workerRef = (EntityReference)queueItem.Attributes["workerid"];
if (workerRef.Id != null && workerRef.Id != context.UserId)
{
throw new InvalidPluginExecutionException("Not allowed to remove queue items picked by other users!");
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
My code bases the retrieval on the Workby field, only if the item is assigned to a user or not assigned, that user can remove the operation.
After testing, my code runs successfully and does what you need, but there is still a point where it can be optimized, you can change the retrieve all columns to show retrieve certain columns.
If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
Best regards,
Cui Hao