Requirement : Prevent Users to Delete the Task if "Owner" of the Task is not the "Created by" of the Task using Plug-in in Dynamics CRM 2011.
On click of Delete button on Ribbon from anywhere Form, Subgrid, HomeGrid, if the Task "Owner" is not Same as Task "Created By" then Owner should be prevented from Deleting the Task with a User Friendly Message.
Implementation:
The Complete Code for the Plug-in to prevent an Owner to Delete the Task:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.CSharp;
using System.Data;
using System.Runtime.Serialization;
using Microsoft.Crm.Sdk.Messages;
namespace TaskDeleteCheck
{
public class PreventTaskDeletion : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.PrimaryEntityName == "task")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
if (context.PreEntityImages.Contains("PreImage"))
{
Entity taskentity = (Entity)context.PreEntityImages["PreImage"];
EntityReference owneridLookup = (EntityReference)taskentity.Attributes["ownerid"];
EntityReference createdbyLookup = (EntityReference)taskentity.Attributes["createdby"];
if (owneridLookup.Id != createdbyLookup.Id)
{
throw new InvalidPluginExecutionException("This task is Created by : " + createdbyLookup.Name + " , User : " + owneridLookup.Name + " is not allowed to Delete the Task.");
}
}
}
else
{
throw new InvalidPluginExecutionException("Activity should be task.");
}
}
}
}
}
The Plugin has to use Pre-Image to compare the values of the fields before transaction happens in the Database.
Now the Registration Process of Plug-in:
Open the Plugin registration Tool, Select your Organization, Log as Admin user (Or if any user has granted permission).
1. Register New Assembly by browsing your dll.
2. You have to Register steps for the plugin on Delete step.
3. Register Pre-Image for the above step.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.CSharp;
using System.Data;
using System.Runtime.Serialization;
using Microsoft.Crm.Sdk.Messages;
namespace TaskDeleteCheck
{
public class PreventTaskDeletion : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.PrimaryEntityName == "task")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
if (context.PreEntityImages.Contains("PreImage"))
{
Entity taskentity = (Entity)context.PreEntityImages["PreImage"];
EntityReference owneridLookup = (EntityReference)taskentity.Attributes["ownerid"];
EntityReference createdbyLookup = (EntityReference)taskentity.Attributes["createdby"];
if (owneridLookup.Id != createdbyLookup.Id)
{
throw new InvalidPluginExecutionException("This task is Created by : " + createdbyLookup.Name + " , User : " + owneridLookup.Name + " is not allowed to Delete the Task.");
}
}
}
else
{
throw new InvalidPluginExecutionException("Activity should be task.");
}
}
}
}
}
The Plugin has to use Pre-Image to compare the values of the fields before transaction happens in the Database.
Now the Registration Process of Plug-in:
Open the Plugin registration Tool, Select your Organization, Log as Admin user (Or if any user has granted permission).
1. Register New Assembly by browsing your dll.
2. You have to Register steps for the plugin on Delete step.
3. Register Pre-Image for the above step.
Once done, you are now Ok to test your business scenario,
Following are the screen shots, on Single Task and Multiple Tasks Closure Process from the CRM Landing Page (In My case Dashboard):
Single Task Selected:
Single Task Selected:




Thanks for the post that helped a lot
ReplyDeleteGlad that i could help ...
ReplyDelete