web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create a plugin to decrypt custom field of Case Entity while Retrieve in MS Dynamics CRM Online

(0) ShareShare
ReportReport
Posted on by

I have a requirement that to Encrypt the custom Field and Decrypt Automatically while viewing the case in MS Dynamics CRM online Portal. I created two Plugins one is for Encrypt at PreCaseCreate and the other is to Decrypt at PostCaseRetrieve.The Plugin for Encryption is Working fine,but plugin for decrypt is not working(which means the encrypted content is not decrypting while viewing in online portal). Below is the code for decryption

protected void ExecutePostCaseRetrieve(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            if (context.Depth == 1)
            {
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
                    // Obtain the target entity from the input parmameters.
                    EntityReference entityreference = (EntityReference)context.InputParameters["Target"];

                    ColumnSet cols = new ColumnSet(new String[] { "title", "description", "new_phicontent" });
                    Entity incident = service.Retrieve(entityreference.LogicalName, entityreference.Id, cols);
                    if (incident.LogicalName.ToLower().Equals("incident"))
                    {
                        try
                        {
                            if (incident.Attributes.Contains("new_phicontent"))
                            {
                                string PHIContent = incident.Attributes["new_phicontent"].ToString();

                                byte[] bInput = Convert.FromBase64String(PHIContent);
                                UTF8Encoding UTF8 = new UTF8Encoding();
                                //Encrypt/Decrypt strings which in turn uses 3DES (Triple Data Encryption standard) algorithm
                                TripleDESCryptoServiceProvider tripledescryptoserviceprovider = new TripleDESCryptoServiceProvider();

                                //Alow to compute a hash value for Encryption/Decryption
                                MD5CryptoServiceProvider md5cryptoserviceprovider = new MD5CryptoServiceProvider();

                                tripledescryptoserviceprovider.Key = md5cryptoserviceprovider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("secretkey"));
                                tripledescryptoserviceprovider.Mode = CipherMode.ECB;
                                ICryptoTransform icryptotransform = tripledescryptoserviceprovider.CreateDecryptor();

                                string DecryptedText = UTF8.GetString(icryptotransform.TransformFinalBlock(bInput, 0, bInput.Length));
                                incident.Attributes["new_phicontent"] = DecryptedText;
                                //service.Update(incident);
                            }

                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    }
                }
            }
        }


I tried with PreCaseRetrieve event also,but i didn't got result

If i uncomment " service.Update(incident); " then decryption is happen.But the Update method permanently decrypting  the text, which means the update is happen in Database level.

My Actual Requirement is to decrypt the content to show user only(if not mess of characters will create confuse to the user).

Kindly help me to resolve this.

  

I have a requirement that to Encrypt the custom Field and Decrypt Automatically while viewing the case in MS Dynamics CRM online Portal. I created two Plugins one is for Encrypt at PreCaseCreate and the other is to Decrypt at PostCaseRetrieve.The Plugin for Encryption is Working fine,but plugin for decrypt is not working(which means the encrypted content is not decrypting while viewing in online portal). Below is the code for decryption

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Mohan,

    I'm confused by the implementation - if you're encrypting fields to store in the database but decrypting them on retrieve, what good is having the values encrypted at all? If you only want the data visible to external website users, you need to take it off the Retrieve because anybody will be able to see it in CRM just from opening the record. You should move the decrypt logic to the website, so the only place the plain text is seen is where the desired user accesses it.

    Hope this helps! Let me know if I misunderstood how you're using this. I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Hi Aiden,

    Actually client raises a case from external Website with some confidential details. We have to encrypt those details and create a case in CRM Server.

    We have agents who can login through dynamics CRM online portal and go through the each case and try to resolve those. In this process the agents has to see the confidential details. This is the reason that I want to decrypt the content while retrieving the case if I didn’t,  then mess of characters will shows and create confuse to the agent.

    This is what my actual requirement. I succeed with attached code only if i uncomment the “service.Update(incident);"  line. But Update methods permanently updating the entity .So Please provide me any alternative solution.

  • Suggested answer
    abhishek_255 Profile Picture
    1,614 on at

    Plugin will not help in this case. You can create web service and call it in java script form load, in request send encrypted value,  service will decrypt & send actual text in response. Then load the service response in respective controls.

  • Community Member Profile Picture
    on at

    Hi, abhishek thanks for your response.

    Actually i am new to CRM, can you please provide some sample code or any reference link

  • Community Member Profile Picture
    on at

    Hi, abhishek thanks for your response.

    Actually i am new to CRM, can you please provide some sample code or any reference link

  • Verified answer
    abhishek_255 Profile Picture
    1,614 on at

    Hi Mohan,

    1. Create a javascript function that will be called on form load

    www.dynamicscrmpros.com/getting-started-with-javascript-form-scripting-for-microsoft-dynamics-crm-2011

    2. Read value from control:

    Xrm.Page.getAttribute('<<fieldname>>').getValue();

    3. Call web service - Ajax

    codehandbook.org/call-asmx-web-service-from-jquery-ajax

    www.codeproject.com/.../Calling-WCF-Services-using-jQuery

    4. Set value returned from webservice in response to control:

    Xrm.Page.getAttribute('<<fieldname>>').setValue('ABC');

    If my response helps you, please mark it as "Verified".

    Best Regards,

    Abhishek

  • abhishek_255 Profile Picture
    1,614 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans