You can use this piece of code to fetch data from Active Directory using JavaScript and then with the help of SPUtility or JavaScript set the value of fields.
function getUserName() {
var wshNetwork = new ActiveXObject("WScript.Network");
var userName = wshNetwork.UserName;
return userName;
}
function getAD(userName) { //var name = userName.split(".");
objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider = "ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;
objCommand.CommandText = "SELECT sAMAccountName, ID, Phone FROM 'LDAP://ad.mycompany.com/OU=User Accounts,OU=User Directory,DC=ad,DC=mycompany,DC=com' WHERE objectCategory='user' and sAMAccountName = '" + userName + "'"; /* Next up is the command itself.*/
objRecordSet = objCommand.Execute(); /* Then we execute the command */ /* Once executed, the command will return an enumeration of the results.*/
var Id, Phone, Name;
if (objRecordSet.RecordCount == 1) {
objRecordSet.Movefirst;
Id= objRecordSet.Fields("Id").value;
Name= objRecordSet.Fields("givenName").value;
Phone= objRecordSet.Fields("Phone").value;
}
else {
Id= "";
Name= "";
Phone= "";
}
objConnection.Close;
return userMail + ";" + firstName + ";" + lastName;
}
var AD = getAD(getUserName()).split(";");
var Id= AD[0];
var Name= AD[1];
var Phone= AD[2];
You can use above variables to set Fields in edit or new form using SPUtility.
For Eg.
SPUtility.GetSPField('Name').SetValue('Hello world!');
For more reference on SPUtility
refer this link.