22 August, 2013

Fetch current login user data from active directory using JavaScript in SharePoint

Problem Description:
Fetch current login user data from active directory using JavaScript in SharePoint.


<script type="text/javascript">
/* getUserName function is use to find Current System User */
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.Open("ADs Provider");

 objCommand = new ActiveXObject("ADODB.Command");
 objCommand.ActiveConnection = objConnection;

 objCommand.CommandText = "SELECT sAMAccountName, givenName, SN, mail,physicalDeliveryOfficeName FROM 'LDAP://DomainName.com/DC=Domain,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 userMail,lastName,firstName;
 if (objRecordSet.RecordCount == 1) {
  userMail = objRecordSet.Fields("mail").value;
  firstName = objRecordSet.Fields("givenName").value;
  lastName = objRecordSet.Fields("sn").value;
  samaccountname= objRecordSet.Fields("samaccountname").value;
  physicalDeliveryOfficeName= objRecordSet.Fields("physicalDeliveryOfficeName").value;
  userMail = "";
  firstName = "";
  lastName = "";
  physicalDeliveryOfficeName = "";
 return userMail+";"+firstName+";"+lastName+";"+physicalDeliveryOfficeName;

var AD = getAD(getUserName()).split(";");

var mail = AD[0];
var firstName = AD[1];
var lastName = AD[2];
var Office = AD[3];

In objCommand.CommandText make connection string properly.

In Select query use column name of active directory from which you want data.
Refer this link for column name:

Product Applies To:
·         SharePoint Server 2010
·         SharePoint Foundation 2010
·         SharePoint Server 2013

Feel free to revert in-case of any query...


  1. when i try to use objConnection in jquery,im getting error "Automation service can not start" - javascript runtime error. Im not able to proceed. Pls guide me.

    1. could you please give some brief details on the same?

      Is this machine specific? did you try on different machine? I would also suggest to give a check on different browser also.

      let me know the results! Thanks for your patience

    2. Every now and again, there's a Windows Update that generates such a bug and another one that follows to fix it and you'll see some of these errors. Yours might be one of these.

      Usually the workaround in-between is to either:

      -go to IE's Network Settings and uncheck "auto detect settings" or any PAC file setting
      -or go to the Java control panel's Network Settings and disable "Use browser settings" or other settings to use a direct connection instead.

    3. -Go to internet options.
      -Select security tab.
      -Under Custom level.
      -Ensure that "Initialize and script active x controls is not marked safe for scripting" select the radio button enabled and try to run your code now

      I am sure ur problem will be solved.

  2. An excellent information provided thanks for all the information i must say great efforts made by you. thanks a lot for all the information you provided.
    Packers And Movers Pune

  3. Hello.

    Why did you copypasted code from my blog without pasting the link to my article? You only added param physicalDeliveryOfficeName, all other variable names are the same. Please, put the link to my initial blogpost.


  4. does it works for SharePoint Online?


  5. this was an exceptionally nice post. Taking a few minutes and actual effort to generate a really good article

    شركة نقل عفش
    ارخص شركة نقل عفش بالرياض

  6. Devops is not a Tool.Devops Is a Practice, Methodology, Culture or process used in an Organization or Company for fast collaboration, integration and communication between Development and Operational Teams. In order to increase, automate the speed of productivity and delivery with reliability.
    python training in bangalore
    aws training in bangalore
    artificial intelligence training in bangalore
    data science training in bangalore
    machine learning training in bangalore
    hadoop training in bangalore
    devops training in bangalore
    iot training in bangalore

  7. Book Best and Verified packers and movers in Pune, Expert in Home Relocation within or outside Pune. Compare 3 quotes from nearby packers and movers in Pune. Get 100% safe & affordable packing moving from Professional Packers and Movers.

    Packers and Movers Pune

  8. It’s actually a nice and helpful piece of information. I’m happy that you shared this useful info with us. Please keep us informed like this. Thank you for sharing.
    packers and movers in shimla


Your feedback is always appreciated. I will try to reply to your queries as soon as possible- Amol Ghuge

Note: Only a member of this blog may post a comment.