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

Community site session details

Session Id :
Dynamics 365 Community / Blogs / CRM Fields / Custom plugin exception out...

Custom plugin exception output for crm form

Micchael Profile Picture Micchael 390

    I was playing with MS Dynamics Crm forms server side validation. The idea was to use a plugin for validation when a lot of calculation involved and JS is not an option. Actually this is a routine task.
   The point here is how we display a response. There is only one way in crm to display a server side exception by throwing InvalidPluginExecutionException. It’s not bad if we could customize the output exception page and make it dynamic.
   I’ve used an embedded file to get configurable html in the run time and have better error message design. I hope it will help you to improve the user experience and add a style to crm form.
Here is the plugin code:
    public class CustomErrorMessage : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
 
            string fileName = "CustomError.xml";
 
            try
            {
                throw new Exception("GO!");
            }
 
            catch (Exception e)
            {
 
                Assembly assembly = Assembly.GetExecutingAssembly();
                using (Stream pageStreamed = assembly.GetManifestResourceStream(this.GetType(), fileName))
                {
                    if(ReferenceEquals(pageStreamed, null)) throw new InvalidPluginExecutionException("Cannot find embeded resource.");
 
                    StreamReader sr = new StreamReader(pageStreamed);
                    string pageString = sr.ReadToEnd();
                    pageString = pageString.Replace("{0}", "This error message set dynamiclly: " + e.Message);
 
                    throw new InvalidPluginExecutionException(String.Format(CultureInfo.InvariantCulture, "Custom exception {0}: {1}", e.Message, pageString), e);
                }
 
            }
        }
    }
 
Here is the output with different styles:
 
 


 

 
    As expected, Java Script doesn’t work and DOM manipulations are unreachable.
 

This was originally posted here.

Comments

*This post is locked for comments