asp.net-mvcentity-frameworkdynamics-crmsitefinity

Working with Entity framework with Sitefinity and Portal Connector and Dynamic CRM


I'm working on a project that contains Dynamics CRM and Portal Connector which built upon Sitefinity.

There is a way to retrieve data inside Portal Connector from Dynamic CRM called Saved Query and this way generate a URL for you to retrieve data by HTTP request in front-end but I don't want to access it by the front end I want to access the Dynamics CRM by Backened, specifically by Entity framework, is it possible to connect to Dynamic CRM by Entity framework and retrieve the data by C# then send it to View?


Solution

  • let me answer my question, in case anyone wants to do a similar thing in the future :

    1- first thing connect to Dynamic CRM is not related to Portal Connector, so the area that you should search in is Dynamic CRM.

    2- To connect to Dynamic CRM you should follow the below steps :
    2.1- install this package "Microsoft.CrmSdk.XrmTooling.CoreAssembly"
    2.2- find what is your connection string.
    2.3 use below code

    
    var service=new CrmServiceClient("AuthType=Office365;Url=https://ititisdf.crm4.dynamics.com;Password=1234" )/*put your connection string instead*/
    

    3- Some example of you could create or retrieved data

    service.Create(new Entity("account"){["name]="Test connection"}); // add record
    
    // retrive data
    
    //1- query expression
    //var query= new QueryExpression().Criteria. <===== from here you can add filteration ... and so on
    
    //2- fetch xml expression
    //var query=new FetchExpression(@"fetch xml value"); // you need to use XrmToolBox to generate your fetchXml
    
    //3- var query=new  QueryByAttribute("account");
    // query.AddAttributeValue("name","Test1");
    
    
    var entities=service.RetrieveMultiple(query).Entities;
    
    
    
    foreach(var entity in entities)
    {
    entity["name"];
    }
    
    var organization=new OrganizationServiceContext(service);
    
    // below code is under a concept called late-bound
    
    var result=(from account in organization.CreateQuery("account")
    join contact in organization.CreateQuery("contact") 
    on account["primarcontactid"] equals contact["contactid"]
    where account["gendercode"] == "test" AND account["industrycode"]=1
    select new {
    
    Name=account["name"],
    ContactName=contact["fullname"]
    }).ToList();
    
    

    // to implement Early bound

    1- go to XrmToolBox ==> About ==> Plugin Store ==> Early Bound Generator==>Early Bound Generator Page will opened choose Entity to skip and choose which entity to want to include and which want to exclude ===> choose the path of generated .cs class that will represent you Entity in your project ===> press on Create Entities ===> now copy the generated file .

    Now you have something like Entity framework :

    Just use Entity name as a normal class :

    var account = new Account{Name="Ahmed"};
    

    and instead of this :

    organization.CreateQuery("account") 
    

    use

    organization.CreateQuery<yourEntityName>()
    

    Actually, I got all of this information from youtube serious related to Dynamic, and here is the link

    note: this serious in the Arabic language for this reason I summarised the steps in this answer to make it helpful for all.