Sharepoint2013/SharePoint2010/SharePoint2007 and JQuery Web Services: DeleteList


I had a requirement today to be able to perform a delete of a list without having to perform a postback. After a little bit of How to Call the SharePoint Web Services with jQuery and a little bit of fine-tuning i came up with a delete list method (deletelist()). Below is just an example of what can be done with the Lists.asmx webservice provided by Sharepoint. Obviously any Web Service can be used or called as required.

01
 
02
function delete_list(listid) {
03
            if (confirm("Are you sure you want to delete this container?") == true) {
04
                var message = listid;
05
                var soapEnv =
06
                "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
07
                    <soapenv:Body> \
08
                        <DeleteList xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
09
                        <listName>" + listid + "</listName>\
10
                        </DeleteList> \
11
                    </soapenv:Body> \
12
                </soapenv:Envelope>";
13
 
14
                $.ajax({
15
                    url: "_vti_bin/lists.asmx",
16
                    beforeSend: function (xhr) {
17
                        xhr.setRequestHeader("SOAPAction",
18
        "http://schemas.microsoft.com/sharepoint/soap/DeleteList");
19
                    },
20
                    type: "POST",
21
                    dataType: "xml",
22
                    data: soapEnv,
23
                    contentType: "text/xml; charset=\"utf-8\"",
24
                    complete: processResult,
25
                    success: function (j) {
26
                        document.location.reload();
27
                    }
28
                });
29
 
30
            } else {
31
                return false;
32
            }
33
        }
34
        function processResult(xData, status) { 
35
            var resultXml = $(xData.responseXML).find("errorstring").text();
36
            resultXml = $.trim(resultXml);
37
            if (resultXml != "") {
38
                alert(resultXml);
39
            }           
40
        }
41