1

I currently have the vacation days for all the employees saved in a SharePoint list. I would like to display the number of vacation days the employee that is logged in has on the Home page. For example, if Sheila is logged in, it will say 30 and if Sheldon is logged in, it will say 50. Is this possible? I know how to retrieve the name of the user that is logged in and I also have that information in the table with the vacation days. I just don't know how to tie it all together to pull the days.

I am guessing that I need to use javascript to query the list based on the user id, and select the field that says 'Vacation Balance' and return that. But I am really not too sure on how to do that. I am very new to SharePoint 2013. Can anyone help me?

2

Yes, you need to use REST api using JQuery to pull the data from vacation list.

Suppose you have two columns in your list, EmployeeVacation:

  1. VacationBalance: No. of vacation days.
  2. EmployeeName: Name of employee(Person or group field).

Then you can use the REST api to get the current user's VacationBalance like given below:

var currentUserId = _spPageContextInfo.userId;

$.ajax({  
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EmployeeVacation')/items?$select=Title,VacationBalance,EmployeeName/Id&$expand=EmployeeName/Id&$filter=EmployeeName/Id eq " + currentUserId,  
    method: "GET",  
    headers: { "Accept": "application/json; odata=verbose" },  
    success: function (data) {
        console.log(data);
    }, 
    error: function (error) {
        console.log(error);
    } 
}) ;

Note: You need to add jQuery file before using this code.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.