Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Status 400 error for creating record in Crm from my web application

(0) ShareShare
ReportReport
Posted on by 35

I am getting status 400 error while creating record in Crm its post method, for get method its working fine, in crm restbuilder its working fine to create record.

string resource = "https://*****.crm4.dynamics.com/";
UserCredential clientredentials = new UserCredential("****t@******onmicrosoft.com", "****@716");
string client_id = "2b572be0-*****-4581-8481-*******";
AuthenticationContext authenticationContext = new AuthenticationContext("">login.microsoftonline.com/.../authorize");
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, client_id, clientredentials);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
httpClient.BaseAddress = new Uri("">orgf65ea8ad.api.crm4.dynamics.com/.../");
account_creation_1 ac_1 = new account_creation_1()
{
name = account_Creation.name,
preetham_reasonforregistration = account_Creation.reason_for_registration,
preetham_DepartmentId = Guid.Parse(account_Creation.Department),
preetham_doctorId = Guid.Parse(account_Creation.Doctor)
};


HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("https://*****.api.crm4.dynamics.com/api/data/v9.2/accounts"))
{
Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;
if (response.IsSuccessStatusCode)
{
Console.Write("Account Created");
}
}

I am getting the access token its everything working fine, anybody have any idea where i am wrong

  • Daniel Schneider Profile Picture
    172 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi,

    httpClient.BaseAddress = new Uri("">orgf65ea8ad.api.crm4.dynamics.com/.../");

    right string of "">?

  • PabloCRP Profile Picture
    1,088 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi, please share what the error says, 

    The Uri for the request should only be the entityNameSet ("accounts","leads",etc..) because your are setting a "BaseAddress" property for you HttpClient Class.

    Also I'd use NewtonJson for converting the account_creation_1 to JSON format, doing this "Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json");" doesn't pass a valid JSON payload.

    for testing purposes in connection to microsoft dataverse take a look of the samples in their Official repo at PowerApps-Samples

    I made SolutionContainer a solution repo base on PowerApps-Samples, where your just clone and play change just this fields by your own.

    private static readonly string Url = "https://contoso.crm.dynamics.com";
    private static readonly string ClientId = "00000000-0000-0000-0000-000000000001";
    private static readonly string UserName = "USER@contoso.onmicrosoft.com";
    private static readonly string Password = "YOUR_PASSWORD";

    here Oficial documentation Docs

    regards.

  • Eduteam srl Profile Picture
    35 on at
    RE: Status 400 error for creating record in Crm from my web application

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("accounts")) //<--when i changed this got error of invalid url

    preetham_DepartmentId = Guid.Parse(account_Creation.Department),//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"

    preetham_doctorId = Guid.Parse(account_Creation.Doctor)//<--yes this is lookup

    but now i had removed, now i am just testing with one field "name" but still got the same error

     account_creation_1 ac_1 = new account_creation_1()

                   {

                       name = account_Creation.name

                   };

                   HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("orgf65ea8ad.api.crm4.dynamics.com/.../accounts"))

                   {

                       Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json")

                   };

                   var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;

    still the same error

  • Suggested answer
    PabloCRP Profile Picture
    1,088 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi Eduteam,

    I'd change this lines

    Payload

    account_creation_1 ac_1 = new account_creation_1()
    {
    name = account_Creation.name,
    preetham_reasonforregistration = account_Creation.reason_for_registration,
    preetham_DepartmentId = Guid.Parse(account_Creation.Department),//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"
    preetham_doctorId = Guid.Parse(account_Creation.Doctor)//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"
    };

    URI

    //wrong
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("https://*****.api.crm4.dynamics.com/api/data/v9.2/accounts"))
    //good
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("accounts"))

    Check for lookup data WebAPi

    regards

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,748 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans