web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Suggested answer
    PabloCRP Profile Picture
    1,088 on at

    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

  • Eduteam srl Profile Picture
    35 on at

    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

  • PabloCRP Profile Picture
    1,088 on at

    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.

  • Daniel Schneider Profile Picture
    172 on at

    Hi,

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

    right string of "">?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans