In last article we saw how to upload a file to the documents library using SharePoint 2013 RESTservice. Now we will see how to delete a file using SharePoint 2013 REST service.
Following is the REST service to delete the file:
"/_api/web/GetFileByServerRelativeUrl('/testdoc/test11.doc')"
This service needs server relative url of file as parameter. Also take care of headers section, refer below code for headers.
Following is the complete code to delete the file:
function DeleteFile()
{
var digest = "";
$.ajax(
{
url: "/_api/contextinfo",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (data) {
}
}).done(function() {
$.ajax(
{
url: "/_api/web/GetFileByServerRelativeUrl('/testdoc/test11.doc')",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": digest,
"IF-MATCH": "*",
"X-HTTP-Method" :"DELETE"
},
success: function (data) {
alert("Delete successfully");
},
error: function (data) {
alert("Error occured." + data.responseText);
}
});
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="DeleteFile()" value="Delete File"> </input>
</div>
</body>
</html>
Following is the REST service to delete the file:
"/_api/web/GetFileByServerRelativeUrl('/testdoc/test11.doc')"
This service needs server relative url of file as parameter. Also take care of headers section, refer below code for headers.
Following is the complete code to delete the file:
function DeleteFile()
{
var digest = "";
$.ajax(
{
url: "/_api/contextinfo",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (data) {
}
}).done(function() {
$.ajax(
{
url: "/_api/web/GetFileByServerRelativeUrl('/testdoc/test11.doc')",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": digest,
"IF-MATCH": "*",
"X-HTTP-Method" :"DELETE"
},
success: function (data) {
alert("Delete successfully");
},
error: function (data) {
alert("Error occured." + data.responseText);
}
});
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="DeleteFile()" value="Delete File"> </input>
</div>
</body>
</html>
No comments:
Post a Comment