none
Jquery / Javascript - Script editor - editform - Check if logged in user is equal to a people picker field RRS feed

  • Question

  • Hi,

    I want to check if the logged in user is same as a form field ( people picker field ). If yes, show another column . I am trying to achieve in EditForm.aspx page

    How to check this in Jquery / JavaScript ?

    Thanks

    Thursday, February 28, 2019 7:02 AM

Answers

  • Hi

    Here's the code for edit form . I have hidden Title field when LoggedIn UserName does not match with Field:User(People Picker).

    ​​<script src="/sites/Shared%20Documents/jquery.min.js" type="text/javascript"></script>

    <script src="/sites/Shared%20Documents/jquery.SPServices-0.7.1a.min.js" type="text/javascript"></script>

    <script type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(test, "sp.js"); function test() { //Hide Title field $('nobr:contains("Title")').closest('tr').hide(); //Get loggedin User Name var userid= _spPageContextInfo.userId; var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")"; var requestHeaders = { "accept" : "application/json;odata=verbose" }; $.ajax({ url : requestUri, contentType : "application/json;odata=verbose", headers : requestHeaders, success : onSuccess, error : onError }); } //Check if User (People Picker) is same as logged in user, if yes then enable Title Field function onSuccess(data, request) { var loginName = data.d.Title; //Get User Name from People Picker Field editorNames = ""; var _PeoplePicker = $("div[title='User']"); var _PeoplePickerTopId = _PeoplePicker.attr('id'); var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId]; editorsInfo = ppobject.GetAllUserInfo(); var i; for (i = 0; i < editorsInfo.length; ++i) { editorNames += editorsInfo[i].DisplayText + ""; } //Check if people picker field value matches with logged in user name if(editorNames == loginName) { //Show Title field $('nobr:contains("Title")').closest('tr').show(); } } function onError(error) { alert("error"); } </script>​​​​​​​​

    Logged In user matches with Title field visible

    Logged In user not matching with Title field hidden

    Thanks


    • Edited by ME_77 Thursday, February 28, 2019 7:50 AM
    • Proposed as answer by ME_77 Thursday, February 28, 2019 7:51 AM
    • Marked as answer by Venkatzeus Thursday, February 28, 2019 11:04 AM
    Thursday, February 28, 2019 7:49 AM