0

I am currently building a leave request app addin for share point using HTML and java script. I want to have each users leave days remaining days displayed on the form when they are submitting a leave request. This value (leave days remaining) will be stored on a separate share point list (wach user will be stored on this list as a people field i know each user has a specific id within share point) and will be different for each user. Is there anyway i can display this specific value based on the user who is currently logged into share point?

This is how the form will look

List i want to take the days remaining value from

2 Answers2

0

Borrowing from this answer, the current user id is available in the global page context info variable.

_spPageContextInfo.userId;

With that, you should be able to filter the list data via the REST API to achieve your objective or via CSOM with the Lists / getItems() method with an appropriate CAML filter.

FourthGenZ28
  • 105
  • 2
  • 9
  • Thank you for your answer, I have tried that using the following method. https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj246019(v%3Doffice.15) only changing the static itemiD variable to _spPageContextInfo.userLoginName; but still have had no luck. – Niall Cooley Dec 28 '18 at 15:11
  • Are you referring to this line: targetListItem = targetList.getItemById(itemId)? If so, that will return a list item by its unique id, not the user ID of a person column. – FourthGenZ28 Dec 28 '18 at 15:16
  • yes that is correct and is the issue i was having as its returning the correct value for the user id, but then i cannot retrieve the list item based on the id as it says the item id does not exist. any ideas on how i can select the list item? – Niall Cooley Dec 28 '18 at 15:22
  • In that case, you will want to use the getItems() method https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj245102(v%3doffice.15) and supply a CAML query to filter by the person column. – FourthGenZ28 Dec 28 '18 at 15:29
  • Thanks for your help I was thinking that to, its just working out the query to use now lol thanks again – Niall Cooley Dec 28 '18 at 15:31
0

We can use the REST API to get list item base on current user from hosted web.

"/_api/web/lists/getbytitle('LeaveEntitlement')/items?$select=DaysRemaining&$filter=UserNameId eq "+_spPageContextInfo.userId

The following code for your reference.

<script type="text/javascript">

var hostweburl;
var appweburl;

// Load the required SharePoint libraries
$(document).ready(function () {
    //Get the URI decoded URLs.
    hostweburl =
        decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
    );
    appweburl =
        decodeURIComponent(
            getQueryStringParameter("SPAppWebUrl")
    );

    // resources are in URLs in the form:
    // web_url/_layouts/15/resource
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);

});

// Function to prepare and issue the request to get
//  SharePoint data
function execCrossDomainRequest() {
    // executor: The RequestExecutor object
    // Initialize the RequestExecutor with the app web URL.
    var executor = new SP.RequestExecutor(appweburl);

    // Issue the call against the app web.
    // To get the title using REST we can hit the endpoint:
    //      appweburl/_api/web/lists/getbytitle('listname')/items
    // The response formats the data in the JSON format.
    // The functions successHandler and errorHandler attend the
    //      sucess and error events respectively.
    executor.executeAsync(
        {
            url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('DaysRemaining')/items?@target='" + hostweburl + "'&$select=DaysRemaining&$filter=UserNameId eq "+_spPageContextInfo.userId,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );
}

// Function to handle the success event.
// Prints the data to the page.
function successHandler(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.results;
    if(results.length>0){
        alert(results[0].DaysRemaining);
    }
}

// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, errorCode, errorMessage){
    console.log("Could not complete cross-domain call: " + errorMessage);
}

// Function to retrieve a query string value.
// For production purposes you may want to use
//  a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}
</script>

Reference: Manipulating list items in SharePoint Hosted Apps using the REST API

LZ_MSFT
  • 6,219
  • 1
  • 7
  • 7