locked
How to use the Microsoft Dynamics CRM OnPremise Web API from your ASP.NET MVC website RRS feed

  • Question

  • Hi,

    I would like to use MS Dynamics CRM OnPremise(2016) Web API from my ASP.NET MVC Website. Please suggest how can I do the same.

    Do we have any sample code snippet which shows the steps to connect to OnPremise Web API.

    I have downloaded the WebAPI metadata from Dynamic CRM and ran the T4 Template(OData v4 Client Code Generator VS Extension) on the same.

    Now how do we connect to MS Dynamic CRM OnPremise(2016) Web API.

    Regards,

    Manoj

    jeudi 6 octobre 2016 13:27

Réponses

  • Hi,

    I have managed to solve this using below code.

    public class HomeController : Controller
        { 
    private HttpClient getNewHttpClient(string userName, string password, string domainName, string webAPIBaseAddress)
            {
                HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(userName, password, domainName) });
                client.BaseAddress = new Uri(webAPIBaseAddress);
                client.Timeout = new TimeSpan(0, 2, 0);
                return client;
            }

            public async Task<ActionResult> GetAssignmentInfo()
            {
                string _serviceUri = "http://devcrm/";
                HttpClient httpClient = getNewHttpClient("crm2016Inst", "Password", "domainname", _serviceUri);
               
                var result = await httpClient.GetAsync(_serviceUri + "api/data/v8.1/accounts?$select=name&$top=3");
             
                var accountInfoJson = await result.Content.ReadAsStringAsync();
                var account = JsonConvert.DeserializeObject<CRMODataConnectionSample.CRMEntity.RootObject<CRMEntity.AccountValue>>(accountInfoJson);
                return Content(account);
    }
        }

    namespace CRMODataConnectionSample.CRMEntity
    {
        public class RootObject<T>
        {
            [JsonProperty("@odata.context")]
            public string context { get; set; }
            public List<T> value { get; set; }
        }

        public class AccountValue : Account // This is CRM Entity name, generated by T4 Template
        {
            [JsonProperty("@odata.etag")]
            public string etag { get; set; }
        }
    }

    Regards,

    • Marqué comme réponse Manoj.Ntz lundi 17 octobre 2016 08:12
    lundi 17 octobre 2016 08:12