The Web-API is new for Microsoft Dynamics 365 (online and on-premises). See more about your Web-API version via CRM => Settings => Customizations => Developer Resources. See more about Dynamics CRM Web API at https://msdn.microsoft.com/en-us/library/gg334767.aspx.
The createRecord method has two arguments:
- entityLogicalName
- data
WebAPI.createRecord(entityLogicalName, data).then(function (data) {});
WebAPI.createRecord("account", {
emailaddress1: "test@company.com"
}).then(function (data) {});
The deleteRecord method has two arguments:
- entityLogicalName
- id
WebAPI.deleteRecord(entityLogicalName, id).then(function (data) {});
WebAPI.deleteRecord("account", "475b158c-541c-e511-80d3-3863bb347ba8").then(function (data) {});
The retrieveRecord method has four arguments:
- entityLogicalName
- id
- options
WebAPI.retrieveRecord(entityLogicalName, id, options).then(function (data) {});
WebAPI.retrieveRecord("account", "475b158c-541c-e511-80d3-3863bb347ba8").then(function (data) {});
The retrieveEntity method has four arguments:
- logicalName
- entityId
- queryOptions (odata query string)
- headers (odata headers)
WebAPI.retrieveEntity(logicalName, entityId, queryOptions, headers).then(function (data) {});
WebAPI.retrieveEntity("account", "475b158c-541c-e511-80d3-3863bb347ba8").then(function (data) {});
The getEntityDefinitions method is used to get the metadata. There is one argument:
- logicalName
WebAPI.getEntityDefinitions(logicalName).then(function(metadata) {});
This method is used by retrieveEntity after finding the entitySetName and can be used when entitySetName is known already. The retrieveEntitySet method has three arguments:
- entitySetName
- entityId
- queryOptions (odata query string)
WebAPI.retrieveEntitySet(entitySetName, entityId, queryOptions).then(function (data) {});
WebAPI.retrieveEntitySet("accounts", "475b158c-541c-e511-80d3-3863bb347ba8").then(function (data) {});
The retrieveMultipleRecords method has three arguments:
- entityLogicalName
- options
- maxPageSize
WebAPI.retrieveMultipleRecords(entityLogicalName, options, maxPageSize).then(function (data) {});
The retrieveMultiple method has two arguments:
- logicalName
- queryOptions (odata query string)
- headers (odata headers)
WebAPI.retrieveMultiple(logicalName, queryOptions, headers).then(function (data) {});
WebAPI.retrieveMultiple("account", {
emailaddress: "test@company.com"
}).then(function (data) {});
WebAPI.retrieveMultiple("account", {}, {
Prefer: "odata.maxpagesize=3"
}).then(function (data) {});
WebAPI.retrieveMultiple("account", {
top: 3
}).then(function (data) {});
The count has one argument:
- logicalName
WebAPI.count(logicalName).then(function (nrEntities) {});
The executeFetchXml has two arguments:
- logicalName
- fetchXml
WebAPI.executeFetchXml(logicalName, fetchXml).then(function (data) {});
WebAPI.executeFetchXml("systemuser", `<fetch>
<entity name='systemuser'>
<attribute name='systemuserid' />
<filter type='and'>
<condition attribute='systemuserid' operator='eq-userid' />
</filter>
</entity>
</fetch>`)
.then(function (data) {});
Enterprise version only
The executeSavedQuery has two arguments:
- logicalName
- queryName
WebAPI.executeSavedQuery("account", "Active Accounts").then(data => {});
Enterprise version only
The executeUserQuery has two arguments:
- logicalName
- queryName
WebAPI.executeUserQuery("account", "HelloWorld").then(data => {});
The updateRecord has four arguments:
- entityLogicalName
- entityId
- attributes
- query (enterprise version only; $select only: see https://msdn.microsoft.com/en-us/library/mt607664.aspx)
WebAPI.updateRecord(entityLogicalName, entityId, attributes, query).then(function (data) {});
WebAPI.updateRecord("account", "475b158c-541c-e511-80d3-3863bb347ba8", {
emailaddress1: "test2@company.com"
}).then(function (data) {});
Bound and unbound actions can be executed via the webAPI. If you don't know which one to use, the executeAction will do the check for you.
WebAPI.executeAction(actionName, data, logicalName, entityId).then(function () {});
See https://msdn.microsoft.com/en-us/library/mt607600.aspx. Unbound action has two arguments:
- actionName
- data
WebAPI.executeUnboundAction(actionName, data).then(function () {});
See https://msdn.microsoft.com/en-us/library/mt607600.aspx. The bound action has four arguments:
- actionName
- data
- logicalName
- entityId
WebAPI.executeBoundAction(actionName, data, logicalName, entityId).then(function () {});
Bound and unbound functions can be executed via the WebAPI. If you don't know which one to use, the executeFunction will do the check for you.
WebAPI.executeFunction(functionString, logicalName, entityId).then(function () {});
See https://msdn.microsoft.com/en-us/library/gg309638.aspx. The execute unbound function has one argument:
- functionString
WebAPI.executeUnboundFunction(functionString).then(function (data) {});
WebAPI.executeUnboundFunction("WhoAmI").then(function (data) {
console.log("UnboundFunction WhoAmI " + JSON.stringify(data));
});
See https://msdn.microsoft.com/en-us/library/gg309638.aspx. The execute bound function has three arguments:
- functionString
- logicalName
- entityId
WebAPI.executeBoundFunction(functionString, logicalName, entityId).then(function () {});
Enterprise version only
Workflows can be executed by WebAPI.executeWorkflow. A workflow is a bound action. See https://msdn.microsoft.com/en-us/library/mt491159.aspx for more information. https://msdn.microsoft.com/en-us/library/mt491159.aspx There are two arguments:
- workflowName
- entityId
WebAPI.executeWorkflow("MyWorkflow", "475b158c-541c-e511-80d3-3863bb347ba8").then(function (data) {
console.log("Workflow MyWorkflow " + JSON.stringify(data));
});
See https://msdn.microsoft.com/en-us/library/mt607875.aspx. Entities can be associated or disassociated using the WebAPI.
The associateEntities has five arguments:
- logicalName
- entityId
- navigationProperty
- associateEntity
- associateEntityId
WebAPI.associateEntities(logicalName, entityId, navigationProperty,
associateEntity, associateEntityId).then(function () {});
The disassociateEntities has four arguments:
- logicalName
- entityId
- navigationProperty
- associateEntityId
WebAPI.disassociateEntities(logicalName, entityId,
navigationProperty, associateEntityId).then(function () {});
The getEntitySetName has one argument:
- logicalName
WebAPI.getEntitySetName(logicalName).then(function (entitySetName) {});
WebAPI.getEntitySetName("account").then(function (entitySetName) {
console.log(entitySetName); //accounts
});
Enterprise version only
The batch has two arguments:
- changeSets
- getSet
The result of changeSets is a boolean set to indicate whether it succeeded. The result of getSet is the data like retrieveMultiple.
WebAPI.batch(changeSets, getSets).then(function (result) {
var changeSetsResult = result[0];
var getSetResult = result[1];
});
var changeSet1 = [{
logicalName: "account",
attributes: {
accountid: "475b158c-541c-e511-80d3-3863bb347ba8",
emailaddress1: "hallo@abc.nl"
}
}, {
logicalName: "account",
attributes: {
accountid: "475b158c-541c-e511-80d3-3863bb347ba8",
emailaddress1: "olla@abc.nl"
}
}];
var changeSet2 = [{
logicalName: "account",
attributes: {
accountid: "475b158c-541c-e511-80d3-3863bb347ba8",
emailaddress1: "ollahoe@abc.nl"
}
}];
var getSet = [{
logicalName: "account"
}, {
logicalName: "systemuser"
}];
WebAPI.batch([changeSet1, changeSet2], getSet).then(function (result) {
var changeSetsResult = result[0]; //[[true, true], [true]]
var getSetResult = result[1]; //[accountData, systemuserData]
});
It's also possible to reference in a changeSet. User reference instead of logicalName
WebAPI.batch([[{
logicalName: "account",
attributes: {
accountid: "475b158c-541c-e511-80d3-3863bb347ba8",
emailaddress1: "hallo@abc.nl"
}
}, {
reference: "$0/Orders",
attributes: {
name: "x"
}
}]]).then(function (data) {});
Odata.include-annotations can be passed using the 'Prefer' header.
WebAPI.retrieveMultiple("account", null, {
Prefer: `odata.include-annotations=*`
}).then(data => {});
Following include-annotations are CRM specific. More can be found on in odata documentation on the web.
- Microsoft.Dynamics.CRM.lookuplogicalname
- Microsoft.Dynamics.CRM.associatednavigationproperty
- OData.Community.Display.V1.FormattedValue
- *
OptionSet for a specific entity attribute can be received for both Picklist as well as Boolean(Two Option) attributes. The OptionSet will be mapped to a key-value object.
WebAPI.getOptionSet("account", "industrycode").then(function (options) {
options.forEach(function (option) {
console.log(option.label + " " + option.value);
});
}).catch(function (e) {
throw e;
});