Tuesday, 28 February 2017

Generating PDF using ITextSharp dll in sandbox solution

Recently I got one requirement where I need to create PDF file and upload it as an attachment to list item from a Sandbox solution

Normally we can create PDF file using ITextSharp dll without any issue in Farm solution.

But in sandbox solution, this solution gave below errors
  1.  Assembly could not load error as we are adding external dll to sandbox
  2.  System.Security.SecurityException: exception because ITextSharp assembly does not allow partially trusted callers 
So first we need to update the dll to allow partial trust calls and then should add it to solution package

Make ITextSharp dll partial trust:

  1.   Download ITextSharp solution  Download iTextSharp
  2.  Open AssemblyInfo.cs of ‘ITextsharp-src-core’ project
  3.  Add below line in AssemblyInfo.cs
           [assembly: System.Security.AllowPartiallyTrustedCallers]
 Recompile solution and take latest itextsharp dll

Now add this dll to sandbox solution bin folder and add it in project references

Add dll to Solution package

Next step is to use the package configuration in order to deploy this assembly to Sandbox GAC cache. All 3rd party assemblies should be configured in sandbox solution package

1.     Open solution package and add dll from bin folder


Rebuild and deploy the sandbox solution. 

Now you can use ITextSharp methods to create PDFs




No comments:

Post a Comment