Get Current User Details from SharePoint Profile Store using Javascript

When you want to use the below script on your application, change the ID's of the TEXTFIELD

<script type="text/Javascript>
    SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "SP.js");
   
function runMyCode() {
        var ctx = new SP.ClientContext.get_current();
        var web = ctx.get_web();
        ctx.load(web);
        var user = web.get_currentUser();
        user.retrieve();
        ctx.executeQueryAsync(
  function () {
      document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff21_ctl00_ctl00_TextField").innerText = user.get_title();
      document.getElementById('ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff21_ctl00_ctl00_TextField').readOnly = "readonly";
      document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff41_ctl00_ctl00_TextField").innerText = user.get_email();
      document.getElementById('ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff41_ctl00_ctl00_TextField').readOnly = "readonly";
      bindUsrDtls(user.get_id());
  },
        function (data) {
            //notify the failure
        });
    }

    function bindUsrDtls(usrName) {
        var personProperties;
        SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties(usrName), 'SP.UserProfiles.js');
    }

    function getUserProperties(useId) {
        var targetUser = useId;
        var clientContext = new SP.ClientContext.get_current();
        this.web = clientContext.get_web();
        clientContext.load(this.web);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' + '<Value Type=\'Number\'>' + useId + '</Value></Eq>' + '</Where></Query><RowLimit>1</RowLimit></View>');
        var userInfoList = this.web.get_siteUserInfoList();
        this.collListItem = userInfoList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded(sender, args) {
        var item = collListItem.itemAt(0);
        if (item.get_fieldValues().Department != null) {
            document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff31_ctl00_ctl00_TextField").innerText = item.get_fieldValues().Department;
            document.getElementById('ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff31_ctl00_ctl00_TextField').readOnly = "readonly";
        }
        if (item.get_fieldValues().WorkPhone != null) {
            document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff51_ctl00_ctl00_TextField").innerText = item.get_fieldValues().WorkPhone;
        }
        if (item.get_fieldValues().MobilePhone != null) {
            document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff61_ctl00_ctl00_TextField").innerText = item.get_fieldValues().MobilePhone;
        }
        if (item.get_fieldValues().Office != null) {
            document.getElementById("ctl00_m_g_ef1f7edf_a38d_4a18_bd9a_647d0c847d62_ff71_ctl00_ctl00_TextField").innerText = item.get_fieldValues().Office;
        }
    }

    function onQueryFailed(sender, args) {
        $get("results").innerHTML = "Error: " + args.get_message();
    }


</script>

Comments

Popular posts from this blog

Activating a SharePoint Feature on Multiple Sites or Site Collections using PowerShell

Managed Path with WildCard and Explicit Inclusion

List All SharePoint 2010 PowerShell Commands