none
How to retrieve & populate: Logged User's Email & Phone ? RRS feed

  • Question

  • Hello Forum

    In my Office 365 (SharePoint Online) I have a very simple Custom List which consists of a bunch of columns/fields, and I have a requirement that when a user clicks on: Add New Item, The logged user's name or email address will be populated into a PeoplePicker field (EmployeeName), and the logged user's phone number will be populated into another SingleLineText field called (EmployeePhone). Other fields will be filled manually by the user.

    How can I accomplish this? Could you please share with me some code/scripts?

    For getting the user\s email address and populating it into a PeoplePicker field (EmployeeName) I have used the script below. But, I am not happy with it as it only works with IE, But, NOT other browsers.

    <script type="text/javascript" src="/jquery-1.12.0.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                ExecuteOrDelayUntilScriptLoaded(setLoginName, "clientpeoplepicker.js");
            });

            function setLoginName() {
                var loginName = _spPageContextInfo.userLoginName;
                var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='EmployeeName']");
                var peoplePickerEditor = peoplePickerDiv.find("[title='EmployeeName']");
                var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
                peoplePickerEditor.val(loginName);
                spPeoplePicker.AddUnresolvedUserFromEditor(true);
            }
         </script>


    I would really appreciate if you could help me to get these two values from user's profile, Delve, or anywhere as long as the code is something I can put inside a Script Editor web part.

    Thank you very much.

        
    Friday, December 30, 2016 6:50 PM

All replies

  • Hi,

    You could try to get user profile by REST api, for example:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='i:0%23.f|membership|vardhaman@siteurl.onmicrosoft.com'

    You could check below thread for more details.

    http://www.vrdmn.com/2016/06/sharepoint-online-get-userprofile.html

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 2, 2017 2:50 AM
  • I couldn't use this.

    I am looking for a code snippet that I can put inside a Script Editor web part on the list's form where each logged user opens the list's form, the user's name or email with their phone will be populated and filled inside two fields.

    Monday, January 2, 2017 7:00 AM
  • Hi,

    The URL just shows the endpoint, you could try to check the thread which I mentioned above for code snippet.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 2, 2017 7:49 AM
  • That what I meant.

    I couldn't use this http://www.vrdmn.com/2016/06/sharepoint-online-get-userprofile.html

    I am looking for a code snippet that I can put inside a Script Editor web part on the list's form where each logged user opens the list's form, the user's name or email with their phone will be populated and filled inside two fields.


    Monday, January 2, 2017 3:35 PM
  • Hi,

    Try this, you could check below thread for more details:

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
    var currentUser;
    function init(){
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.clientContext.load(currentUser);
        this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
    }
    
    function onQuerySucceeded() {
        document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
        document.getElementById('userId').innerHTML = currentUser.get_id();
        document.getElementById('userTitle').innerHTML = currentUser.get_title();
        document.getElementById('userEmail').innerHTML = currentUser.get_email();
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    }
    </script>

    http://sharepoint.stackexchange.com/questions/73032/get-current-user-in-client-object-model-with-javascript

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, January 3, 2017 1:35 AM