locked
Restrict a entity record from deleting using plugin RRS feed

  • Question

  • hey guys ,

                    I want to Restrict a entity record from deleting using plugin.Neither entity record should be deleted nor enity should be..How to do this .Please give me a sample code

    Friday, July 27, 2012 6:47 AM

Answers

  • hey i registered it to delete handle of dev_ssn...Now what exactly the code will be for my task?....please provide me sample code for an example

    Do you want me to do your work for free?

    public void Execute(IServiceProvider ServiceProvider)

            {
                try
                {
                    IPluginExecutionContext ObjIpligunExecutionContext = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));

                    IOrganizationServiceFactory objIOrganizationServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService CrmService = objIOrganizationServiceFactory.CreateOrganizationService(ObjIpligunExecutionContext.UserId); Entity dev_ssn; if (ObjIpligunExecutionContext.InputParameters.Contains("Target") && ObjIpligunExecutionContext.InputParameters["Target"] is Entity) { dev_ssn = (Entity)ObjIpligunExecutionContext.InputParameters["Target"]; if (ObjIpligunExecutionContext.MessageName == "Create") { throw new InvalidPluginExecutionException("Sorry,You can't create the record!"); } if (ObjIpligunExecutionContext.MessageName == "Update") { throw new InvalidPluginExecutionException("Sorry,You can't update the record!"); } } if (ObjIpligunExecutionContext.InputParameters.Contains("Target") && ObjIpligunExecutionContext.InputParameters["Target"] is EntityReference) { if (ObjIpligunExecutionContext.MessageName == "Delete") { throw new InvalidPluginExecutionException("Sorry,You can't delete the record!"); } } } catch (Exception ex) { throw ex; } }



    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Friday, July 27, 2012 8:19 AM
    Moderator

All replies

  • Hello,

    Idea of plugin is simple.

    1. Plugin should handle Delete message in Pre mode.

    2. Plugin should recheck the possibility of deletion of the record.

    3. In case it is not allowed - plugin just should throw nan exception like:

    throw new Exception("It is not allowed to delete this record!");


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Friday, July 27, 2012 7:01 AM
    Moderator
  • my sample code for handling delete message is 

     if (ObjIpligunExecutionContext.MessageName == "Delete")
                        {
                            throw new InvalidPluginExecutionException("Sorry,You can't delete the record!");
                        }

    but its not working when i delete the record..please provide me sample code for an example

    Friday, July 27, 2012 7:07 AM
  • Can you please provide full code of your plugin and screenshots of registration?

    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Friday, July 27, 2012 7:13 AM
    Moderator
  •  

    Code:

    public void Execute(IServiceProvider ServiceProvider)

            {
                try
                {
                    IPluginExecutionContext ObjIpligunExecutionContext = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));

                    IOrganizationServiceFactory objIOrganizationServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));

                    IOrganizationService CrmService = objIOrganizationServiceFactory.CreateOrganizationService(ObjIpligunExecutionContext.UserId);

                    Entity dev_ssn;

                    if (ObjIpligunExecutionContext.InputParameters.Contains("Target") &&
                        ObjIpligunExecutionContext.InputParameters["Target"] is Entity)
                    {

                        dev_ssn = (Entity)ObjIpligunExecutionContext.InputParameters["Target"];


                        if (ObjIpligunExecutionContext.MessageName == "Create")
                        {
                            throw new InvalidPluginExecutionException("Sorry,You can't create the record!");
                        }

                        if (ObjIpligunExecutionContext.MessageName == "Update")
                        {
                            throw new InvalidPluginExecutionException("Sorry,You can't update the record!");
                        }

                        if (ObjIpligunExecutionContext.MessageName == "Delete")
                        {
                            throw new InvalidPluginExecutionException("Sorry,You can't delete the record!");
                        }
                        if (ObjIpligunExecutionContext.MessageName == "RemoveItem")
                        {
                            throw new InvalidPluginExecutionException("Sorry,You can't delete the record!");
                        }



                    }



                }
                catch (Exception ex)
                {
                    throw ex;
                }


             }

                                                                                    

    Friday, July 27, 2012 7:18 AM
  • Based on your screenshot I can tell that your plugin handles Create message. You should register it to handle Delete.

    Also when you delete record Target is EntityReference, so

     if (ObjIpligunExecutionContext.InputParameters.Contains("Target") &&
                        ObjIpligunExecutionContext.InputParameters["Target"] is Entity)

    you would not never get inside this condition.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Friday, July 27, 2012 7:21 AM
    Moderator
  • hey i registered it to delete handle of dev_ssn...Now what exactly the code will be for my task?....please provide me sample code for an example
    Friday, July 27, 2012 8:15 AM
  • hey i registered it to delete handle of dev_ssn...Now what exactly the code will be for my task?....please provide me sample code for an example

    Do you want me to do your work for free?

    public void Execute(IServiceProvider ServiceProvider)

            {
                try
                {
                    IPluginExecutionContext ObjIpligunExecutionContext = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));

                    IOrganizationServiceFactory objIOrganizationServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService CrmService = objIOrganizationServiceFactory.CreateOrganizationService(ObjIpligunExecutionContext.UserId); Entity dev_ssn; if (ObjIpligunExecutionContext.InputParameters.Contains("Target") && ObjIpligunExecutionContext.InputParameters["Target"] is Entity) { dev_ssn = (Entity)ObjIpligunExecutionContext.InputParameters["Target"]; if (ObjIpligunExecutionContext.MessageName == "Create") { throw new InvalidPluginExecutionException("Sorry,You can't create the record!"); } if (ObjIpligunExecutionContext.MessageName == "Update") { throw new InvalidPluginExecutionException("Sorry,You can't update the record!"); } } if (ObjIpligunExecutionContext.InputParameters.Contains("Target") && ObjIpligunExecutionContext.InputParameters["Target"] is EntityReference) { if (ObjIpligunExecutionContext.MessageName == "Delete") { throw new InvalidPluginExecutionException("Sorry,You can't delete the record!"); } } } catch (Exception ex) { throw ex; } }



    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Friday, July 27, 2012 8:19 AM
    Moderator
  • hey still facing the error ,

    download log is

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #82664784Detail: 

    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220970</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #82664784</Message>
      <Timestamp>2012-07-27T08:24:57.3241183Z</Timestamp>
      <InnerFault>
        <ErrorCode>-2147220891</ErrorCode>
        <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
        <Message>An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.</Message>
        <Timestamp>2012-07-27T08:24:57.3241183Z</Timestamp>
        <InnerFault i:nil="true" />
        <TraceText>

    [ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityClose]
    [b14b8737-a6d7-e111-9ae1-002655875bb9: ActivityFeeds.Plugins.ActivityClose: Delete of dev_ssn]


    </TraceText>
      </InnerFault>
      <TraceText i:nil="true" />
    </OrganizationServiceFault>

    Error msg is :An error occurred .Please contact to...............


    Friday, July 27, 2012 8:26 AM
  • Fine...got the error....its running well now ..Thanks
    Friday, July 27, 2012 8:31 AM