Monday, June 15, 2015

OData query operations in SharePoint 2013 Online REST requests

Scenario: Get the employee(logged user) leave balance
var userId = _spPageContextInfo.userId;
  //Retrive the leave balance of the current user--Start
  $.ajax({
   
        url:_spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('Leave Balance')/items?$select=Balance&$filter=(EmployeeId eq '" + userId + "') ",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
//alert(data.d.results[0].Balance);
if(data.d.results.length>0)
$( "input[id*='Balance']" ).val( data.d.results[0].Balance);
 else
 $("
There is issue to retrieve the leave balance. Please contact HR!
").dialog();
        },
        error: function () {
            $("
There is issue to retrieve the leave balance. Please contact HR!
").dialog();
        }
    });
  //Retrive the leave balance of the current user--End
Ex1:
var URL1=_spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('LeaveRequests')/items?$select=Title&$filter=(Title eq '"+user.full_name+"' and ((Approval_x0020_Status ne 'Cancel') and  (Approval_x0020_Status ne 'Approved') and  (Approval_x0020_Status ne 'Rejected')))";


No comments: