优化JSOM查询

时间:2017-12-19 07:41:02

标签: sharepoint-2013 sharepoint-jsom sharepoint-2016

我有以下SharePoint 2016 JSOM查询,该查询根据当前用户电子邮件检索列表项...此查询当前有效,但是,我一直在使用jquery defferred和Promised对象进行一些研究。为了理解这些方法的工作原理,我想优化下面的现有代码

<script type="text/javascript" src="//code.jquery.com/jquery-3.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function () {


    getCurrent();
    //getMyTasks();
    $('#checkall').click(function (e) {
    $("#GetItems").find('td input:input[name=chk]').prop('checked', this.checked);
    if($(this).is(':checked')) {
          $('#Reject').removeAttr('disabled');
          $('#Approve').removeAttr('disabled');
        } else {
          $('#Reject').attr('disabled', true);
          $('#Approve').attr('disabled', true);
        }
});
    $('#GetItems').on('change', 'input[name=chk]', function(){
        if($(this).is(':checked')) {
          $('#Reject').removeAttr('disabled');
          $('#Approve').removeAttr('disabled');
        } else {
          $('#Reject').attr('disabled', true);
          $('#Approve').attr('disabled', true);
        }
      });
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', approve);
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', reject);
  // approve();
   //reject();


});

function getCurrent()


{
    $.ajax({  

            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email",  
            headers: { Accept: "application/json;odata=verbose" },  
            success: function (data) {  
                currentEmail=data.d.Email; 

                getMyTasks();
                } ,  
            error: function (data) {  
                alert("failed to get email");  
            }  
        });  
}
function getMyTasks(){

    //var listTitle="hardware%20requisition";
    var row = "";  
var buttonrow="";
    var url =_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EUS Asset Request')/Items?$select=ID,AssetMgrEmail,EUSPersonMailID,SRNo,AssetAvailability,Asset_x0020_Manager_x0020_Approv,Asset_x0020_Manager_x0020_Commen,Attachments,Department,Department_x0020_Head,Department_x0020_head_x0020_Appr,Department_x0020_head_x0020_appr1,Department_x0020_head_x0020_Appr0,HOD_x0020_Approval_x0020_Comment,IT_x0020_Head,IT_x0020_Head_x0020_Approval_x001,IT_x0020_head_x0020_Approval_x00,Items_x0020_Requested_x0020_For,IT_x0020_head_x0020_approval_x000,IT_x0020_head_x0020_Approved_x00,Job_x0020_Title,L1_x0020_Approval_x0020_Comments,L1_x0020_Approval_x0020_Date,L1_x0020_Approval_x0020_Status,L1_x0020_Approved_x0020_Date,L1_x0020_Supervisor_x0020_Email,Motivation,OPCO,Replacement_x0020_Type,Request_x0020_Summary,Request_x0020_Type,Requester_x0020_Name,EUSPersonMailID,Requester_x0020_Phone_x0020_Numb,ViewID,Author/Name,Author/EMail,Author/Title&$expand=Author/Id&$filter=EUSPersonMailID eq '"+currentEmail+"' and L1_x0020_Approval_x0020_Status eq 'Approved' and Department_x0020_head_x0020_appr1 eq 'Approved' and IT_x0020_head_x0020_approval_x000 eq 'Approved' and Asset_x0020_Manager_x0020_Approv eq 'Approved' and EUSTeamConfirmation eq 'Pending'"
    $.ajax({  
        url: url,  
        type: "GET",  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
        },  
        success: function(data){  
            var result = data.d.results;  
            if(result.length>0) 
            {

                    $("#divbuttons").show();

            $.each(result, function(key, item)  
            {  
                var createdDate= moment(item.Created_x0020_Date).format("DD/MM/YYYY");

                row = row + '<tr style="background-color: #cccccc;"><td style="width: 261.6px;"><strong>Select</strong> <input name="chk" type="checkbox" value='+ item.Id+' /></td><td style="width: 151.2px;">&nbsp;</td><td style="width: 324.8px;"><strong>Request ID: </strong>'+item.ID+'</td><td style="width: 324.8px;"><strong>Requester Name: </strong>'+item.Author.Title+'</td><td style="width: 376.8px;"><strong>Phone Number:</strong><br />'+item.Requester_x0020_Phone_x0020_Numb+'</td><td style="width: 396px;"><strong>Department:</strong><br />'+item.Department+'</td><td style="width: 213px;"><strong>Request Type</strong><br />:'+item.Request_x0020_Type+'</td><td style="width: 4.8px;">Availability Status<br/>:'+item.AssetAvailability+'</td><td style="width: 213px;"><strong>Serial Number</strong><br />'+item.SRNo+'</td><td style="width: 213px;"><strong>EUS Person MailID</strong><br />'+item.EUSPersonMailID+'</td><td style="width: 213px;"><strong>Receiver MailID</strong><br />'+item.Author.EMail+'</td></tr>';

            })  

            $("#GetItems>tbody").html(row);  
            $("#GetItems").show();  
        }
        else{
            $("#NoTask").show(); 

        }
        },  
        error: function(data){  
            alert("Failed to get list items.");  
        }  
    });
}
function approve(){
    $("#Approve").click(function(event){


        event.preventDefault();
        var searchIDs = $("#GetItems input:checkbox:checked").map(function(){
          return $(this).val();
        }).get(); 

        var itemArray = [];

        var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle("EUS Asset Request");

        for(var i = 0; i< searchIDs.length; i++){    
           // alert($("#GetItems textarea#"+searchIDs[i]+"").val());
            var oListItem = oList.getItemById(searchIDs[i]); 

            oListItem.set_item('EUSTeamConfirmation', 'Confirmed');
            oListItem.set_item('ViewID', 'FinalVw');


            oListItem.update();
            itemArray[i] = oListItem;
            clientContext.load(itemArray[i]);

            }
            clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

        function onQuerySucceeded() {
    alert('Items Updated');

    location.reload(true);

}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

    location.reload(true);
}

    });

}

function reject(){
    $("#Reject").click(function(event){
        event.preventDefault();
        var searchIDs = $("#GetItems input:checkbox:checked").map(function(){
          return $(this).val();
        }).get(); 

        var itemArray = [];

        var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle("EUS Asset Request");

        for(var i = 0; i< searchIDs.length; i++){    

            var oListItem = oList.getItemById(searchIDs[i]); 

            oListItem.set_item('EUSTeamConfirmation', 'Rejected');
            oListItem.set_item('ViewID', 'RejectVw');


            oListItem.update();
            itemArray[i] = oListItem;
            clientContext.load(itemArray[i]);

            }
            clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

        function onQuerySucceeded() {
    alert('Task Rejected');
   location.reload(true);
   //getCurrent();

}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    location.reload(true);
   // getCurrent();
}

    });

}
</script>

<div id="NoTask" style="display:none">You don't have pending tasks for approval</div>

<div style="display:none" id="divbuttons"><input type="checkbox" id="checkall"></input><button type="button" id="Reject" disabled>Reject</button><button type="button" id="Approve" disabled>Approve</button></div>
<table id="GetItems" cellspacing="12"><tbody></tbody> </table>

请帮助

0 个答案:

没有答案