{Solved}Why are my CRM SDK Assembly references in Azure functions throwing error? How do I refer my custom assemblies which are not in Nuget?

And this was the complaint from one my customer. Why CRM assembly references in Azure function apps are throwing error? To add to my woes, that was some pretty important functionality running inside those small little functions and alas, the last few runs have failed. It was few months back and at that point of time, I fixed it.

But in a recent training on WebHooks, it seemed to me participants are not aware of this scenario and hence penning this blog for the benefit of my readers.

If you are absolutely new to Azure functions and how to add assembly references in Azure functions, I guess a bit of basic understanding before reading this blog would help.

If you would have developed Azure functions and referenced CRM SDK assemblies, you would done using project.json file. You would have created the file and then added the below JSON in the file to reference CRM SDK’s

{
"frameworks": {
"net461":{
"dependencies": {
"Microsoft.CrmSdk.CoreAssemblies": "9.0.0.7"
}
}
}
}

With azure functions 1.0, this would have worked just fine. At runtime, the framework would read your project.json file and restore the assemblies from Nuget. A detailed description on this by friend and fellow MVP Nishant Rana in this blog.

But the issue starts once Azure functions are upgraded to V2.0. And why? Because project.json is no longer supported in V2.0. Instead a new file – function.proj needs to created and referenced assemblies need to be defined in specific format.

All we need is to add a file with name “function.proj” and put the below contents.

image

<Project Sdk="Microsoft.NET.Sdk"> 
    < PropertyGroup> 
        < TargetFramework>net461</TargetFramework> 
    </PropertyGroup>    
    < ItemGroup> 
        < PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" Version="9.0.0.7" /> 
    </ItemGroup> 
< /Project>

Your function code remains unchanged. But for beginners, you would need to reference with #r directive before using it.

image

Now once you run, you will see that the packages are now successfully restored and the application is working fine.

P.S – I tried at that time with .NET framework 4.6.2 and using <TargetFramework>net462</TargetFramework>. Unfortunately with this version it threw an error. May be I am missing something silly and I would be grateful if readers point this out.

Ok, so my CRM Sdk assemblies are in Nu-get and I can refer them just fine in the new function.proj file. But what about custom assemblies which are not in Nuget. How do I refer them? Well, if that’s the case, you should not be worried. There is an easy way to do this.

For this example, let’s assume Microsoft.Xrm.Sdk.dll is my custom assembly.

In your functions app, navigate to Platform Features –> Advanced Tools (Kudu)

Once inside Kudu, open Powershell prompt

image

Navigate to Site –> wwwroot. Once inside the wwwroot folder, create a new folder called bin

image

image

Go inside bin folder and drag and drop your assembly. In my case it is Microsoft.Xrm.Sdk.dll

image

All set and done. Now we just need to refer this assembly in our code file. As shown in the below screenshot, we can refer it by using a relative path.

image

Now you can start using your assembly. Simple isn’t it?

Honestly this part is not something related to specifically to D365 but with Microsoft’s vision of removing technology barriers, a CRM architect/ developer would  frequently find themselves working on Azure functions and this might be some information which saves some time.

Hope this helps!

Cheers!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)

Advertisements

{Solved}–Invalid Argument error while showing Virtual entity using Custom Virtual Entity Data Provider on Unified interface

Virtual entities are there for pretty long time now. And why this done and dusted topic? Sometimes the greatest of surprises come from the simplest of stuffs and this was one such scenario.

So here was my customer IT team who had developed a pretty good custom virtual entity data provider to show up virtual entities in CRM. And they had done it a year back and it was working perfectly fine till they decided to make the switch to Unified interface. While the virtual entity is showing up just fine in classic UI, the Unified interface is throwing an error – “Invalid Argument”

To be honest, my first thought was there must have been some changes made to the code but again if that is the case, how come it is still working in Unified Interface. Searched the heck out of Google but nothing much on the Unified interface. So I decided to try something very simple of my own.

I created a custom Virtual entity data provider and wrote a very simple RetrieveMultiple plugin. No dynamic data and all. Just some adhoc records being returned.

public class RetrieveMultiplePlugin : IPlugin 
    { 
        public void Execute(IServiceProvider serviceProvider) 
        { 
             var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 
            context.OutputParameters["BusinessEntityCollection"] = this.GetEntities(); 
        }

        public EntityCollection GetEntities() 
         { 
            var entCollection = new EntityCollection(); 
            
            for(var i=0; i < 5; i++) 
            { 
                var ent = new Entity("virtual_demoentity"); 
                ent["virtual_demoentityid"] = Guid.NewGuid(); 
                ent["virtual_name"] = $"Record {i + 1}";

                entCollection.Entities.AddRange(ent); 
            }

            return entCollection; 
        } 
    }

As simple as above. Can’t believe anything can be simpler than this. Registered the assembly as Custom Data provider in plugin registration tool. Set up the virtual entity in CRM. Went to classic UI and I could see my five hardcoded records right up there. Piece of cake.

image

Not that I can say of when I navigated to the same Virtual entity on Unified interface.

image

And finally from one expert, got hold of a piece of code which was working. Honestly when I started comparing, for first few passes could not even find the reason. Finally one line caught the eye. And I  made the change from

var entCollection = new EntityCollection();

to

var entCollection = new EntityCollection( { EntityName = “virtual_demoentity”} );

Uploaded it now and it ran just fine in Unified interface as well.

image

So remember, if you are getting this error in Unified interface after designing your Virtual entity data provider and the same is working fine in classic UI, make sure you double check that while creating the instance of EntityCollection, you specify the “EntityName” property.

Hope this helps and saves you some hours for this silly one.

Cheers!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)