How to use XRM in C#?

517 Views Asked by At

Hello I need to convert a JS Xrm web api function into C#

var opp = Xrm.Page.data.entity.getId();
  Xrm.WebApi.retrieveMultipleRecords(
    "dei_opportunityline",
    "?$select=_dei_vendor_value,dei_multiplier,dei_iscommission,dei_opportunitylineid&$filter=_dei_opportunity_value eq " +
      opp
  ).then(
    function success(result) {
      for (var i = 0; i < result.entities.length; i++) {
        var newMulti = null;
        if (result.entities[i].dei_iscommission) {
          if (
            result.entities[i]["_dei_vendor_value"] ==
            "5fc8d03b-c41c-eb11-a813-000d3a31ed8d"
          ) {
            if (jcimulti != null) newMulti = jcimulti;
          } else {
            if (commulti != null) newMulti = commulti;
          }

For methods like .retrieveMultipleRecords, result.entities[i].dei_iscomission

is there a way to write this in C# plugin ?!

2

There are 2 best solutions below

0
Arun Vinoth-Precog Tech - MVP On

You cannot use Xrm in C#, this is useful inside Dynamics client scripting (JS) context only. ie web form context.

For Server side (C#), you have to use HttpClient. Sample code

        HttpClient httpClient = null;
        httpClient = new HttpClient();
        //Default Request Headers needed to be added in the HttpClient Object
        httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
        httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //Set the Authorization header with the Access Token received specifying the Credentials
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

        httpClient.BaseAddress = new Uri("https://yourorg.api.crmx.dynamics.com/api/data/v9.0/");
        var response = httpClient.GetAsync("dei_opportunityline?$select=_dei_vendor_value,dei_multiplier,dei_iscommission,dei_opportunitylineid&$filter=_dei_opportunity_value eq <guid>").Result;
        if (response.IsSuccessStatusCode)
        {
            var records = response.Content.ReadAsStringAsync().Result;

            //you can get dei_iscomission from above records  
        }

This is how we use in console application, but in plugin or workflow code - you can use Org Service to get data using Query Expression or fetchxml. Methods like service.RetrieveMultiple will suffice your need. Read more

4
Illeris On

Simply include the crmsdk nuget package in C#. Then you can connect like you want. See docs

This packages gives you access to the official c# SDK for XRM. the methods are much easier to use then directly calling the ODATA Web API. For an example to fetch multiple records, see https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/246665/how-to-retrieve-multiple-records-from-an-entity-in-crm-using-c For a quick start, see https://learn.microsoft.com/en-us/powerapps/developer/data-platform/webapi/get-started-dynamics-365-web-api-csharp