jQuery and Sharepoint Web Services: GetListItems with queries

The business case for this, was to add a context menu item based on an item property, in my case, document status.


Essentially this method was called just before the item is added to the context menu item, it is intercepted at the Custom_AddListMenuItems method which allows you to add list menu items. In the method below, i use the local ID of the conext menu item clicked, together with the list guid (ctx.listname) and i query the list for the document status of that item.
I only add the menu item when docstatus is not equal to published. Another interesting note is the ajax call is done synchronously which forces the program halt execution until the ajax call has received a response from the web service.

01

02
function ShowModerationHistory(currentItemID, r, t, m, v, y) {
03
 
04
    var returnValue = false;
05
    var soapEnv =
06
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
07
                <soapenv:Body> \
08
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
09
                        <listName>" + ctx.listName + "</listName>" +
10
			            "<viewFields>" +
11
			            "<ViewFields>" +
12
		                    "<FieldRef Name='DocumentStatus' />" +
13
			            "</ViewFields>" +
14
     			        "</viewFields>" +
15
                            "<query>" +
16
			                    "<Query>" +
17
                                    "<Where>" +
18
                                    "<Eq>" +
19
                                    "<FieldRef Name='ID' />" +
20
                                    "<Value Type='Integer'>" + currentItemID + "</Value>" +
21
                                    "</Eq>" +
22
                                    "</Where>" +
23
                                "</Query>" +
24
                            "</query> " +
25
                        "</GetListItems> " +
26
                    "</soapenv:Body> " +
27
                "</soapenv:Envelope>";
28
    $.ajax({
29
        url: SP.PageContextInfo.get_webServerRelativeUrl() + "/_vti_bin/lists.asmx",
30
        type: "POST",
31
        dataType: "xml",
32
        data: soapEnv,
33
        async: false,
34
        complete: function (xData, status) {
35
            var docStatus = $(xData.responseXML).find("z\\:row:eq(0)").attr("ows_DocumentStatus");
36
            if (docStatus != null && docStatus != undefined) {
37
                if (docStatus.toUpperCase() != "PUBLISHED") {
38
                    x = CIMOpt(r, t, m, v, null, y);
39
                }
40
            }
41
        },
42
        beforeSend: function (xhr) {
43
            xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/GetListItems');
44
        },
45
        contentType: "text/xml; charset=\"utf-8\""
46
    });
47
 
48
}