Wednesday 5 June 2013

Update ListItems using Webservice

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>

<script type="text/javascript">
   
    mySoftwareListGUID = "9B164628-736A-427D-A0CB-D21F2167EAD2";
    mySoftwareViewGUID = "32A08E36-310B-4322-9A8A-A88F9867424A";
    // function to retrieve data from the Software to Review (Auth & Unauth) List
   
     $(document).ready(function ()
      {
        var soapEnvMS ="<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>{" + mySoftwareListGUID + "}</listName> \
                        <viewName>{" + mySoftwareViewGUID + "}</viewName> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";
        $.ajax({
            url: listsWSURL,
            type: "POST",
            dataType: "xml",
            data: soapEnvMS,
            complete: processResultMS,
            contentType: "text/xml; charset=\"utf-8\""
        });
    });
    // function called when data is retrieved.  Processes data and renders HTML table
   
   function processResultMS(xData, status)
     {
        if ($(xData.responseXML).find("z\\:row").length == 0)
        {
            var msg = '<div id=\"SAM27\">There is no software to display in this section. Please check the other tab. You do not need to perform the software review due to one of the following reasons:<ol><li>You only have standard/core applications installed on your machine (no further action needed)</li><li>You are a member of a group that share a machine (no further action needed)</li><li>We do not have information regarding your software (no further action needed for the moment)</li></ul></div>';
            $('#SAMAuthorisedUnauthorisedList').append(msg);
        }
        else
           {
            var tableHeader = '<table class=\"SAMList\"><tr><th style=\"text-align: left;\"><div id=\"SAM22\">Application</div></th><th><div id=\"SAM23\">Version</div></th><th><div id=\"SAM24\">I need this to do my job</div></th><th><div id=\"SAM25\">I don\'t need this to do my job</div></th></tr>';
            var tableFooter = '</table>';
            var tableRows = '';
            $(xData.responseXML).find("z\\:row").each(function () {
                var applicationName = $(this).attr("ows_Title");
                var applicationPublisher = $(this).attr("ows_Publisher");
                var applicationVersion = $(this).attr("ows_Application_x0020_Version");
                //var machineID = $(this).attr("ows_MachineID");
                //var userName = $(this).attr("ows_Username");
                //var status = $(this).attr("ows__Status");
                var itemID = $(this).attr("ows_ID");
                var needed = $(this).attr("ows_Needed");
                var notNeeded = $(this).attr("ows_NotNeeded");
                needed = buildCheckboxMS(itemID, needed, 'Needed', '\'#' + itemID + '_NotNeeded\'');
                notNeeded = buildCheckboxMS(itemID, notNeeded, 'NotNeeded', '\'#' + itemID + '_Needed\'');
                tableRows += '<tr><td>' + applicationPublisher + ' ' + applicationName + '</td><td>' + applicationVersion + '</td><td style=\"text-align: center;\">' + needed + '</td><td style=\"text-align: center;\">' + notNeeded + '</td></tr>';
            });
            $('#SAMAuthorisedUnauthorisedList').append(tableHeader + tableRows + tableFooter);
        }
    }
    // function to create Checkboxes with initial checked value
   
       function buildCheckboxMS(id, initialValue, field, otherID)
       {
        if (initialValue == 1)
        {
            return '<input class=\"SAMMSCheckBox\" type=\'checkbox\' id=\'' + id + '_' + field + '\' value=\'' + id + '_' + field + '\' onclick=\"checkOrUnCheckCheckbox(this, ' + otherID + ')\" checked />';
        }
        else
        {
            return '<input class=\"SAMMSCheckBox\" type=\'checkbox\' id=\'' + id + '_' + field + '\' value=\'' + id + '_' + field + '\' onclick=\"checkOrUnCheckCheckbox(this, ' + otherID + ')\" />';
        }
    }
    // function to handle the mutually exclusive checkboxes
   
function checkOrUnCheckCheckbox(id, otherID)
{
        if (id.checked)
        {
            $(otherID).removeAttr("checked");
        }
    }
    // function to build each row of the Write-back XML payload
   
function buildMSMethodPayload(methodId, id, value)
   {
        var field = id.split('_')[1];
        id = id.split('_')[0];
        return "<Method ID='" + methodId + "' Cmd='Update'><Field Name='ID'>" + id + "</Field><Field Name='" + field + "'>" + value + "</Field></Method>";
    }
    // function to perform the Write-back of data
   
    function updateListItemMS()
    {
        var soapEnvMSWriteBackHeader =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                    <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>{" + mySoftwareListGUID + "}</listName> \
                        <updates> \
                            <Batch OnError='Continue' ListVersion='1' ViewName='" + mySoftwareViewGUID + "'>";
        var soapEnvMSWriteBackPayload = "";
        var count = 1;
        $('.SAMMSCheckBox').each(function () {
            soapEnvMSWriteBackPayload += buildMSMethodPayload(count++, this.value, this.checked);
        });
        var soapEnvMSWriteBackFooter =
                            "</Batch> \
                        </updates> \
                    </UpdateListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";
        var soapEnvMSWriteBack = soapEnvMSWriteBackHeader + soapEnvMSWriteBackPayload + soapEnvMSWriteBackFooter;
        $.ajax({
            url: listsWSURL,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("SOAPAction",
                "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
            },
            type: "POST",
            dataType: "xml",
            data: soapEnvMSWriteBack,
            complete: processResultMSWriteBack,
            contentType: "text/xml; charset=\"utf-8\""
        });
    }
    // function called when Write-back completes.  Shows outcome message
   
function processResultMSWriteBack(xData, status)
   {
        var msg = $('#SAM28').text();
        if (status != "success") {
            msg = $('#SAM29').text();
        }
        alert(msg);
    }
</script>
<div id="SAM20" class="SAMHeading">Software to review</div>
<br />
<div id="SAM21">Please confirm if you need the software listed below to do your job. Use the scroll bar to navigate.<br /><br />If the software is no longer required you can either remove this now or it will be removed for you at a later stage.<br /><br />If you need help removing software, you can find an easy to follow guide in our 'How to Guides' area above, or you can get help by selecting 'Request Help' above.<br /><br /><strong>When finished please click 'save'</strong></div>
<br />
<div class="SAMButtonContainer"><input id="SAM32" class="SAMButton" type="button" value="Save" onclick="javascript: updateListItemMS()" /></div>
<div id="SAMAuthorisedUnauthorisedList" class="SAMList"></div>
<br />
<div class="SAMButtonContainer"><input id="SAM26" class="SAMButton" type="button" value="Save" onclick="javascript: updateListItemMS()" /></div>
<div style="display: none;" id="SAM28">Thank you for taking the time to participate in the Software Review</div>
<div style="display: none;" id="SAM29">An error has occurred while saving your updates.  Please reload the page and re-enter your changes</div>

</body>
</html>

No comments:

Post a Comment