Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Unable to use HtmlAgilityPack in Plugin C#

(2) ShareShare
ReportReport
Posted on by
I have a requirement to parse an Html table from a field into Json. I want to assign the table content separately to another fields.
I installed HtmlAgility Pack v 1.11.72 from nuget and am using below code:
 var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var tableNode = doc.DocumentNode.SelectSingleNode(///table/);
            if (tableNode != null)
            {
                var rows = tableNode.SelectNodes(/.//tr/);
            }
 
I don't get any error during compilation. But once the assembly is update, and plugin executes, I am getting following error.
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Could not load file or assembly 'HtmlAgilityPack, Version=1.11.72.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' or one of its dependencies. The system cannot find the file specified. (Fault Detail is equal to Exception details:
ErrorCode: 0x80040265
Message: Could not load file or assembly 'HtmlAgilityPack, Version=1.11.72.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' or one of its dependencies. The system cannot find the file specified.
TimeStamp: 2024-01-29T10:41:40.1669805Z
OriginalException: PluginExecution
ExceptionSource: PluginExecution
 
I have checked after the code build, the htmlagility dll is present in the bin folder. I am unable to understand the issue, please help me
  • CU24010953-0 Profile Picture
    2 on at
    Unable to use HtmlAgilityPack in Plugin C#

    The issue could be related to missing or improperly loaded dependencies during the plugin execution in your CRM environment. Here are some steps to troubleshoot and resolve the problem:

    1. Ensure Proper Assembly Deployment:
      Verify that the HtmlAgilityPack.dll is correctly deployed to the environment where the plugin is executing. For CRM plugins, dependencies must be included in the deployment package if they are not part of the GAC (Global Assembly Cache).

    2. Merge the Dependency (ILMerge or ILRepack):
      Since CRM plugins run in a sandboxed environment, you can use tools like ILMerge or ILRepack to merge the HtmlAgilityPack assembly into your plugin assembly. This ensures all dependencies are bundled into a single DLL.

    3. Check the CRM Assembly Isolation:
      If your plugin is registered in "Isolation Mode" (Sandbox), ensure all referenced assemblies are compatible with sandboxed plugins. The sandbox environment restricts certain operations, and external dependencies can sometimes cause issues.

    4. Confirm Assembly Version and Token:
      Double-check the version, culture, and public key token of the HtmlAgilityPack assembly. Ensure they match the version you've referenced in your project. Mismatched assembly versions can cause this error. Receiptify

    5. Add the Assembly to the GAC (if applicable):
      If your CRM environment is not sandboxed and you have access to the server, consider adding HtmlAgilityPack.dll to the GAC. This ensures the assembly is available globally.

    6. Enable Detailed Logging:
      Add detailed logging to your plugin to capture more information about the error. Log details like the full stack trace, current working directory, and loaded assemblies.

    7. Test Locally:
      Run the same plugin code in a local environment outside the CRM sandbox to ensure the logic functions correctly.

    8. Alternative Solution:
      If merging or deploying the dependency is not feasible, consider using built-in .NET libraries like System.Xml or System.Text.Json to parse the HTML if your requirement can be simplified.

    Example with ILMerge:

    If you decide to use ILMerge, here's a simple guide:

    1. Install ILMerge using NuGet or download it.
    2. Use the following command to merge your assemblies:
      ILMerge /out:YourPluginMerged.dll YourPlugin.dll HtmlAgilityPack.dll
    3. Deploy the merged DLL to your CRM environment.

    Let me know if you need further guidance or code examples for any of these steps!

  • Suggested answer
    Nitesh Raj Profile Picture
    142 on at
    Unable to use HtmlAgilityPack in Plugin C#

    The error message you're encountering suggests that Dynamics 365 is unable to find or load the HtmlAgilityPack assembly when your plugin executes, even though it's present in the bin folder during local builds. This issue commonly occurs in Dynamics 365 plugins due to assembly loading restrictions and deployment nuances.

     

    Possible Causes and Solutions:

     
     

    1. Missing Assembly in Plugin Registration

     

    Issue:

    Even though the DLL is in your local bin folder, Dynamics 365 plugins run in a sandboxed environment, which does not have access to external libraries unless they are explicitly included during deployment.

     

    Solution:

    Make sure that the HtmlAgilityPack.dll is included in the Plugin Assembly during registration. You can do this in one of two ways:

     

    1. ILMerge or ILRepack (Recommended for sandboxed environments)

     

    Merge the HtmlAgilityPack DLL with your plugin assembly to ensure it is embedded within your main DLL.

     

    Steps:

     
    1. Install ILRepack via NuGet:
      Install-Package ILRepack
      
    2. Add a post-build event to merge dependencies:
      ilrepack /out:YourPluginMerged.dll YourPlugin.dll HtmlAgilityPack.dll
      
    3. Register the merged YourPluginMerged.dll to Dynamics 365. 
     
     

    2. Embedding the Assembly in Your DLL

     

    Instead of external DLL references, embed the HtmlAgilityPack within your plugin assembly by:

     

    1. In Visual Studio, right-click the HtmlAgilityPack.dll reference in Solution Explorer, and set Copy to Output Directory = Copy Always.

    2. Modify your .csproj file to embed the DLL as a resource:
      <ItemGroup>
          <EmbeddedResource Include="HtmlAgilityPack.dll" />
      </ItemGroup>
      

    3. Load the embedded resource at runtime within your plugin:
      using System.Reflection;
      using System.IO;
      public static Assembly LoadEmbeddedAssembly()
      {
          var resourceName = "YourNamespace.HtmlAgilityPack.dll";
          using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
          {
              byte[] assemblyData = new byte[stream.Length];
              stream.Read(assemblyData, 0, assemblyData.Length);
              return Assembly.Load(assemblyData);
          }
      }
      

    4. Register the plugin with the embedded DLL.

         
       
       

      Ensure Proper Version and Binding Redirects

       

      If your Dynamics 365 instance might be using an older version of the library, you can ensure compatibility using a binding redirect in the app.config:

       
      <configuration>
        <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
              <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="bd319b19eaf3b43a" culture="neutral" />
              <bindingRedirect oldVersion="0.0.0.0-1.11.72.0" newVersion="1.11.72.0" />
            </dependentAssembly>
          </assemblyBinding>
        </runtime>
      </configuration>
      
       

       


    5.  

       

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans