User Profiles basically contain information (Manager, WorkEmail, workPhone,Department and etc.) about the person in the organization.
Create SharePoint Controls as mentioned below.
- Name (People or Person)
- E-Mail (Single Line of Text)
- Phone Number (Single Line of Text)
Add Script Editor webpart into NewForm.apsx page and write the below code into script editor webpart.
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
- <script type="text/javascript" src="/_layouts/15/SP.js"></script>
- <script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.01/jquery.SPServices.min.js"></script>
-
- $(document).ready(function (){
-
- $("input:text").focus(function(){
- var userId = $("span.ms-entity-resolved").attr("ID");
- userId=userId.replace(/\\/g,'\\');
- userId = userId.split("|")[1];
- $().SPServices({
- operation: "GetUserProfileByName",
- async: false,
- AccountName: userId,
- completefunc: function (xData, Status) {
- workEmail = getUPValue(xData.responseXML, "WorkEmail");
- workPhone = getUPValue(xData.responseXML, "WorkPhone");
- }
- });
- $("input[title='E-Mail']").val(workEmail);
- $("input[title='Phone Number']").val(workPhone);
- });
-
- });
-
- function getUPValue(x, p) {
- var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
- return $(this).find("Name").text() == p;
- }).find("Values").text();
- return thisValue;
- }
In the below screen, EMail and Phone Field Values get auto populated onfocus of any control in the form.