Perform additional data operations using the OData endpoint

 

Applies To: Dynamics CRM 2015

In addition to basic data operations, you can use the REST endpoint for Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update to perform some more specialized data operations.

In This Topic

Setting complex types to null

Using Deep insert

Updating individual properties

Associating and disassociating records

Setting complex types to null

In order to set fc3ade34-9c4e-4c33-88a4-aa3842c5eee1#BKMK_CRMComplexTypes to null, you must explicitly set each primitive type within them to null.

For example, to set an fc3ade34-9c4e-4c33-88a4-aa3842c5eee1#BKMK_EntityReference type to null, you must set the Id, the LogicalName, and the Name properties to null. The same is true for fc3ade34-9c4e-4c33-88a4-aa3842c5eee1#BKMK_OptionSetValue, fc3ade34-9c4e-4c33-88a4-aa3842c5eee1#BKMK_Money, and fc3ade34-9c4e-4c33-88a4-aa3842c5eee1#BKMK_BooleanManagedProperty.

Using Deep insert

Deep insert is a technique in which you can create multiple new related records in the same operation.

The following sample shows JavaScriptcode that uses jQuery to define an account and two related tasks. For more information, see 1bb82714-1bd6-4ea4-8faf-93bf29cabaad#BKMK_UsingJQuery. When the tasks are assigned to the Account_Tasks property representing related tasks and submitted by using a POST request to the /AccountSet URI, the account and the two related tasks are created in a single operation:

var account = new Object();
account.Name = "Sample Account";

var task1 = new Object();
task1.Subject = "Sample Task 1";

var task2 = new Object();
task2.Subject = "Sample Task 2";

var tasks = new Array();
tasks.push(task1);
tasks.push(task2);

account.Account_Tasks = tasks;

var jsonAccount = window.JSON.stringify(account);

$.ajax({ type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: ODataPath + "/AccountSet",
    data: jsonAccount,
    beforeSend: function (XMLHttpRequest) {
        //Specifying this header ensures that the results will be returned as JSON.
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    success: function (data, textStatus, XmlHttpRequest) {},
    error: function (XMLHttpRequest, textStatus, errorThrown) {}
});

Deep insert works from either side of an entity relationship. The previous example created new tasks in the context of a new account record. You can reverse this to create a new account record in the context of creating a new related task by submitting a POST request to the /TaskSet URI.

Updating individual properties

You can update individual attribute values for a record by using a HTTP PUT request using a URI that addresses a specific attribute and passing the new value in the body of the request.

/AccountSet(guid'c2d26b79-7496-df11-a7c2-00155dba380d')/Name

Using this URI in an HTTP PUT request with a new name for the account in the body will update the value of only that property.

Associating and disassociating records

There are two ways to associate or disassociate records: by updating the data in the entity reference properties just as if they were any other type of property or through the URL of the link resource.

When associating or disassociating records on the many side of an N:1 entity relationship, use the $links URI. For N:N and 1:N entity relationships use the URI without $links.

The following sample shows that the URI will return a URI for any opportunity records associated with the account record specified in the URI:

/AccountSet(guid'c2d26b79-7496-df11-a7c2-00155dba380d')/$links/opportunity_customer_accounts

If you want to associate an existing opportunity record with this account, you must use this URI in a HTTP POST request that includes the URI for that opportunity in the body.

Note

When associating a record on the many side of a N:1 entity relationship, any existing value will be overwritten when the reference only supports a single value.

Similarly, to disassociate an opportunity, you must use an HTTP DELETE request that includes a reference to a specific link resource. The following sample shows that the URI represents a specific account record associated to another account by using ParentAccountId entity reference property:

/AccountSet(guid'c2d26b79-7496-df11-a7c2-00155dba380d')/$links/Referencedaccount_parent_account(guid'b0e5a4a6-8996-df11-a7c2-00155dba380d')

A HTTP DELETE request against this URI will remove the association.

Note

Some entity relationships are required. You will get an error if you attempt to delete data for a required relationship.

See Also

Perform basic data operations using the OData endpoint
Use the OData endpoint with web resources
Use the OData endpoint with Ajax and JScript web resources
Sample: Associate and disassociate records using the OData endpoint with JavaScript

© 2016 Microsoft. All rights reserved. Copyright