How to delete document/attachment by using Rest API

Kimsan MRP asked on May 30, 2017 04:18

Dear Kentico supporter,

Could you please help me how to delete document/attachment by using Rest API?

Best Regards, Kimsan

Recent Answers


Anton Grekhovodov answered on May 30, 2017 06:28

To delete documents, you should send request using DELETE http method to following URLs (Documentation Deleting documents):
~/rest/content/currentsite/<culture>/document/<alias path>
~/rest/content/site/<site name>/<culture>/document/<alias path>

Attachments are Kentico objects, so to delete them, you should send DELETE request to the next URLs (Documentation Deleting objects):
~/rest/cms.attachment/<id>
~/rest/cms.attachment/<guid>

1 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 30, 2017 11:13 (last edited on May 30, 2017 11:15)

Hi Kimsan,

First you need to enable and ensure your REST API is configured in your kentico application #Configure Rest Services. In the Admin/Settings/Integration/REST settings, you will want to make sure you set the following:

Service Enabled

Make sure the service is enabled on your site!

Authentication mode

In my example, I will be using “Basic Authentication.” You can also choose Forms Authentication, as well as set a hash for the URL.

Object access is read only

This will determine if users will be able to create objects, rather than only reading them. You will want to make sure that “objects” are NOT set to read only.

Allow sensitive fields for administrators

This setting will allow us to set the password for the new user account. Only Global Administrators are allowed to do this action.

Rest Configuration snapshot

Then run any rest url to get data to test rest is enabled and working fine.

Example: http://localhost/KenticoCMS/rest/cms.country

Output: All cms.country objects retrieved in XML format.

Then you can move forward with various data manipulation option using REST API.

Use your logic to perform delete operation using rest API

example:

Delete by object ID

YourRestURL/ObjectName/ID

Delete by object GUID

YourRestURL/ObjectName/GUID

URL: http://localhost/Kentico10/rest/cms.attachment/332

Using AjaxRequest

$.ajax({
    url: 'http://localhost/Kentico10/rest/cms.attachment/332',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});

Thanks

Happy to help you

1 votesVote for this answer Mark as a Correct answer

Kimsan MRP answered on May 30, 2017 11:42 (last edited on May 30, 2017 11:44)

Dear All ,

I am using C# code to request url like this

    static void DeleteDocument(Guid attachmentGUID)
    {
        string httpMethod = "DELETE"; // "GET", "POST", "PUT" or "DELETE"
        string requestUrl = string.Format("http://localhost/rest/cms.attachment/{0}", attachmentGUID); // The URL of the REST request (base URL + resource path)
        // Creates the REST request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
        // Sets the HTTP method of the request
        request.Method = httpMethod;
        // Authorizes the request using Basic authentication
        request.Headers.Add("Authorization: Basic YWRtaW5pc3RyYXRvcjo=");

    }
    But it doesn't work, So could you please correct me to the right way?
0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 30, 2017 12:57

You should add the following code to the end of your method to perform the request:

var response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
   // some code if needed
}
0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 30, 2017 14:15 (last edited on May 30, 2017 14:17)

Hi Kimsan,

I got your problem why this not working because your requeest is not Authorized you must need to set a valid Authorization header.In below code I'm using administrator as user to test your case, you can use your user or access token or type of Authorization.

static void DeleteDocument(Guid attachmentGUID)
    {
        string requestBaseUrl = "http://localhost/rest";     
        string username = "administrator";
        string password = "";
        string result = "";
        try
        {
            requestBaseUrl = string.Format("{0}/cms.attachment/{1}", requestBaseUrl, attachmentGUID); // The URL of the REST request (base URL + resource path)

            // Creates the REST request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestBaseUrl);

            // Sets the HTTP method of the request
            request.Method = "DELETE";

            // Authorizes the request using Basic authentication
            // request.Headers.Add("Authorization: Basic YWRtaW5pc3RyYXRvcjo=");

            // Authorizes the request using Basic authentication
            string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));

            request.Headers.Add("Authorization", "Basic " + svcCredentials);

            // Gets the REST response
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            result = "Succesfully deleted";
        }
        catch (Exception ex)
        {
            //
            result = "Something went wrong";
        }

    }

Best practice to use try , catch for error handling and tracing.

Thanks

Happy to help you

0 votesVote for this answer Mark as a Correct answer

Kimsan MRP answered on June 7, 2017 06:21

Dear all,

I got stuck in this line

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I always got error "The remote server returned an error: (403) Forbidden"

I am trying many way, but I still couldn't solve it well.

Could you please give me the best solution to fix this?

Best Regards, Kimsan,

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on June 7, 2017 12:47

The error means that you don't have enough permissions for your request. Try to check IIS configuration and settings in Kentico (Configuring REST service in IIS and Authenticating REST requests)

0 votesVote for this answer Mark as a Correct answer

Kimsan MRP answered on June 8, 2017 03:06 (last edited on June 8, 2017 03:11)

Dear All,

I have fixed it. All you both answer are correct just following step by step.

Thank you very much for your help.

Best Regards,

Kimsan

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.