Answered by:
Pre Delete Plugin

Question
-
Hi All,
I need to Restrict user from deleting a record ,
how do i do that ,
Can anybody send me an example of Pre Delete Plugin ...
Thanks,Wednesday, August 26, 2009 6:55 AM
Answers
-
Hi Rahul,
It could be something like this
public void Execute(IPluginExecutionContext context) { DynamicEntity entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]; // check for certain conditions if (entity.Properties.Contains("fieldname")) { entity.Properties.Remove("fieldname"); } else { // cancel delete operation with proper message throw new InvalidPluginExecutionException("You don't have sufficient rights to delete the record! "); } }
Regards,
Nishant Rana
http://nishantrana.wordpress.com- Marked as answer by DavidJennawayMVP, Moderator Tuesday, October 13, 2009 3:47 PM
Wednesday, August 26, 2009 7:05 AM
All replies
-
Hi Rahul,
It could be something like this
public void Execute(IPluginExecutionContext context) { DynamicEntity entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]; // check for certain conditions if (entity.Properties.Contains("fieldname")) { entity.Properties.Remove("fieldname"); } else { // cancel delete operation with proper message throw new InvalidPluginExecutionException("You don't have sufficient rights to delete the record! "); } }
Regards,
Nishant Rana
http://nishantrana.wordpress.com- Marked as answer by DavidJennawayMVP, Moderator Tuesday, October 13, 2009 3:47 PM
Wednesday, August 26, 2009 7:05 AM -
Hi.
You can Restrict user from deleting a record with Security Roles which user have.
Truth is opened the prepared mind My blog - http://a33ik.blogspot.comWednesday, August 26, 2009 7:09 AMModerator -
Hi try this code
Public Sub Execute(ByVal context As Microsoft.Crm.Sdk.IPluginExecutionContext) Implements Microsoft.Crm.Sdk.IPlugin.Execute Dim source As Moniker Dim objCrmService As CrmSdk.CrmService Dim objAccount As CrmSdk.account If context.InputParameters.Contains("Target") Then source = CType(context.InputParameters("Target"), Moniker) 'Initiate Object instance objCrmService = New CrmSdk.CrmService objAccount = New CrmSdk.account 'Call CRM Service objCrmService = GetCrmSdkService(context) 'Get Account Object objAccount = GetValueOfFieldFromAccount(source, objCrmService, strFilePath) If objAccount IsNot Nothing AndAlso objAccount.new_FieldName <> FieldValue Then Throw New InvalidPluginExecutionException("Error: You cannot delete" & objAccount.name ) End If End If End Sub
Use PreImage on Pre Event for Delete Message
Dare to promise- Proposed as answer by D-Virdi Wednesday, August 26, 2009 7:52 AM
Wednesday, August 26, 2009 7:51 AM -
Hi ,
Delete Message returns a Moniker instead of Dynamic entity, so the above code:
DynamicEntity entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"];
will throw an exception. The Moniker entity however contains the GUID for the entity, so you can use that to fetch the Dynamic entity if required.
Cheers...
Tuesday, May 25, 2010 12:31 AM