Wednesday 28 December 2011

jQuery and Sharepoint Web Services: GetListItems with queries

jQuery and Sharepoint Web Services: GetListItems with queries

// November 23rd, 2010 // Uncategorized
I recently did another post, detailing deleting a list through the use of Sharepoint Web Services. In this post ill give you a codesample on how to query a list using the item ID and the list name.
The business case for this, was to add a context menu item based on an item property, in my case, document status. Manipulating context menu items is a long winded process, one which is best read in posts such as this post or another post here
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.

 function ShowModerationHistory(currentItemID, r, t, m, v, y) {
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
function ShowModerationHistory(currentItemID, r, t, m, v, y) {
    var returnValue = false;
    var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
    $.ajax({
        url: SP.PageContextInfo.get_webServerRelativeUrl() + "/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        async: false,
        complete: function (xData, status) {
            var docStatus = $(xData.responseXML).find("z\\:row:eq(0)").attr("ows_DocumentStatus");
            if (docStatus != null && docStatus != undefined) {
                if (docStatus.toUpperCase() != "PUBLISHED") {
                    x = CIMOpt(r, t, m, v, null, y);
                }
            }
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/GetListItems');
        },
        contentType: "text/xml; charset=\"utf-8\""
    });

No comments:

Post a Comment