Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add a New Contact in Dynamics 365 using PHP

In a Joomla application I am getting a user info as follows and then I need to save the user info as a contact in a Dynamics 365 database through their REST API.

                    $user = JFactory::getUser();
                    $username = $user->username;
                    $name = $user->name;

I have looked up Dynamics documents around Web API and REST API like this and this, but none of them provide useful info how I can call the API to add a new contact. Currently, I am connecting to Dynamics 365 web application via this url: http://example.com:8088/mysite/api/data/v8.2. The linked post also talks about REST API, but only querying. I'm looking for a way to post data to Dynamics CRM using REST API.

like image 382
djnotes Avatar asked Jul 28 '18 11:07

djnotes


People also ask

How do I add data to Dynamics 365?

First, open Visual Studio and Create a new solution and project. Secondly, right click on the project, and select Add>New Item. Thirdly, select Dynamics 365 Items, on the left, and then select 'Runnable Class (Job)' from the list. Fourthly, enter in a name.


1 Answers

The payload to create Contact using crm webapi will look like this: Read more

POST [Organization URI]/api/data/v8.2/contacts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json

{
    "firstname": "Arun",
    "lastname": "Vinoth"
}

Sorry am not from PHP background, but this link may help you.

Update:
I browsed little bit. Found the below code sample from SO answer. Update the [Organization URI] with CRM URL, for ex. https://testorg.crm.dynamics.com

$url = '[Organization URI]/api/data/v8.2/contacts';
$data = array('firstname' => 'Arun', 'lastname' => 'Vinoth');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
like image 146
Arun Vinoth - MVP Avatar answered Oct 06 '22 01:10

Arun Vinoth - MVP