Plugin is not getting fired in Sandbox for OnPremise environment. RRS feed

  • Question

  • Hello Guys,

               I have one plugin which is generating PDF file from an Email.This plugin using third party dll "Itextshartp.dll" for the functionality.We have on-premise instance of crm and we are registering the plugin on the same with Post Operation and Synchronous way.

              When I register and fires this plugin on "none" mode it works fine but it doesnt work for the "Sandbox" mode and generate the error "System.TypeInitializationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #F680793B".

    here is the code whihc I have written in my plugin :


    namespace EmialToPDF
        public class EmailToPDFClass : IPlugin
            static EmailToPDFClass()
                //throw new ArgumentNullException("localContext");

                        //here is the error occures
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnResolveAssembly);

                //AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
            public void Execute(IServiceProvider serviceProvider)

                ITracingService tracingService =

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName != "email")

                    throw new InvalidPluginExecutionException("Hello");

                        // Obtain the organization service reference.
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                        var emailEntity = ((Entity)context.InputParameters["Target"]).ToEntity<Email>();

                        MemoryStream memStream = new MemoryStream();
                        var doc = new Document();

                        var writer = PdfWriter.GetInstance(doc, memStream);
                        PdfPTable table = new PdfPTable(2);

                        table.DefaultCell.Border = 0;

                        float[] widths = new float[] { 1f, 3f };

                        table.AddCell(GetPdfCell("From :"));

                        table.AddCell(GetPdfCell("To :"));

                        table.AddCell(GetPdfCell("Cc :"));

                        foreach (ActivityParty item in emailEntity.Cc)

                        table.AddCell(GetPdfCell("Bcc :"));
                        table.AddCell(GetPdfCell(" "));

                        table.AddCell(GetPdfCell("Subject :"));

                        var BlankCell = GetPdfCell(" ");
                        BlankCell.Colspan = 2;

                        Paragraph para = new Paragraph();
                        StringReader reader = new StringReader(emailEntity.Description);



                        StyleSheet styles = new StyleSheet();
                        styles.LoadTagStyle("th", "size", "9px");
                        styles.LoadTagStyle("th", "face", "Calibri");
                        styles.LoadTagStyle("span", "size", "9px");
                        styles.LoadTagStyle("span", "face", "Calibri");
                        styles.LoadTagStyle("td", "size", "9px");
                        styles.LoadTagStyle("td", "face", "Calibri");

                        using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc))
                            //HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
                            using (var sr = reader)
                                //Parse the HTML


                        //Attachment Code
                        var attachment = new ActivityMimeAttachment();
                        attachment.FileName = "Sample.pdf";
                        attachment.Body = Convert.ToBase64String(memStream.ToArray());
                        attachment.MimeType = "text/plain";
                        attachment.AttachmentNumber = 1;
                        attachment.ObjectTypeCode = "email";
                        var emailId = new Guid(emailEntity.ActivityId.ToString());
                        attachment.ObjectId = new EntityReference("email", emailId);


                    catch (Exception ex)
                        tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

            private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)

                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                AssemblyName assemblyName = new AssemblyName(args.Name);
                String path = assemblyName.Name + ".dll";

                if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
                    path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);

                using (Stream stream = executingAssembly.GetManifestResourceStream(path))
                    if (stream == null)
                        return null;
                    byte[] assemblyRawBytes = new byte[stream.Length];
                    stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
                    return Assembly.Load(assemblyRawBytes);

            private static PdfPCell GetPdfCell(String displayText)
                var font = FontFactory.GetFont("Calibri", 9);
                var cell = new PdfPCell(new Phrase(displayText, font));
                cell.Border = 0;
                return cell;



    The error occures on bold line.

    Any help would be appriciable.Please help ASAP


    Prashant Wani ,Software Engineer-II,Infinx Services Pvt.Ltd.

    Friday, November 21, 2014 12:07 PM

All replies

  • Hi Prashant,

    Check your trace logs for exact error and post it here.

    Regards, Saad

    Friday, November 21, 2014 1:19 PM
  • Hi,

    Sandbox has restirctions restrictions like below.

    • Only the HTTP and HTTPS protocols are allowed.
    • Access to localhost (loopback) is not permitted.
    • IP addresses cannot be used. You must use a named web address that requires DNS name resolution.
    • Anonymous authentication is supported and recommended. There is no provision for prompting the logged on user for credentials or saving those credentials.

    As you see it is running in "NONE" , this is causing because of the external dll.

    Posting the exact error may help to answer your question.

    Thank you,

    Sreeni Pavalla

    Tuesday, November 25, 2014 9:22 AM