Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Writing Delete Request Using Web API
WhatsApp
Mahender Pal
9y
10.5k
0
0
100
Article
In this article, we are going to discuss how we can write delete request using Web API for Dynamics CRM 2016. We can use http
DELETE
method in Web API for different delete scenario. For example, let's say we want to remove value of specific property (attribute) using Web API, so we can simply pass the property name with the record URI like the following:
serverURL+
"/api/data/v8.0/accounts(Record GUID)/propertyname"
So to remove that property value form the record specific to URI, our request could be simple like the following,
var
serverURL=Xrm.Page.context.getClientUrl();
var
req =
new
XMLHttpRequest();
req.open(
"DELETE"
,serverURL+
"/api/data/v8.0/accounts(CBA24BEE-2DA0-E511-80DE-3863BB343AF8)/accountnumber"
,
true
);
//let's say we want to remove account number
req.setRequestHeader(
"Accept"
,
"application/json"
);
req.setRequestHeader(
"Content-Type"
,
"application/json; charset=utf-8"
);
req.setRequestHeader(
"OData-MaxVersion"
,
"4.0"
);
req.setRequestHeader(
"OData-Version"
,
"4.0"
);
req.onreadystatechange =
function
() {
if
(
this
.readyState == 4
/* complete */
) {
req.onreadystatechange =
null
;
if
(
this
.status == 204) {
alert(
'Value removed'
);
}
else
{
var
error = JSON.parse(
this
.response).error;
alert(error.message);
}
}
};
req.send();
But if we want to remove single-valued navigation (lookup) we need to pass the single-valued navigation property name and also need to append
$ref
like the following,
req.open(
"DELETE"
,serverURL+
"/api/data/v8.0/accounts(CBA24BEE-2DA0-E511-80DE-3863BB343AF8)/primarycontactid/$ref"
,
true
);
//name of the navigation property+"$ref"
Above request will remove the lookup value form the record which means association between these records will be removed.
And finally to delete complete record we can just simply pass complete record URI like below:
var
serverURL=Xrm.Page.context.getClientUrl();
var
req =
new
XMLHttpRequest();
req.open(
"DELETE"
,serverURL+
"/api/data/v8.0/accounts(CBA24BEE-2DA0-E511-80DE-3863BB343AF8)"
,
true
);
req.setRequestHeader(
"Accept"
,
"application/json"
);
req.setRequestHeader(
"Content-Type"
,
"application/json; charset=utf-8"
);
req.setRequestHeader(
"OData-MaxVersion"
,
"4.0"
);
req.setRequestHeader(
"OData-Version"
,
"4.0"
);
req.onreadystatechange =
function
() {
if
(
this
.readyState == 4
/* complete */
) {
req.onreadystatechange =
null
;
if
(
this
.status == 204) {
alert(
'Deleted'
);
}
else
{
var
error = JSON.parse(
this
.response).error;
alert(error.message);
}
}
};
req.send();
}
Stay tuned for more Web API Samples!
CRM
Deleting record using Web API
Dynamics CRM
Dynamics CRM 2016
Web API
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
HIMBAP
We are expert in Microsoft Power Platform.
Membership not found