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.

Resolution:

<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.Provider="ADsDSOObject";

 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) {
  objRecordSet.Movefirst;
  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;
 }
 else
 {
  userMail = "";
  firstName = "";
  lastName = "";
  physicalDeliveryOfficeName = "";
 }
 objConnection.Close;
 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];
</script>


References:
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...

6 comments:

  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.

    ReplyDelete
    Replies
    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

      Delete
    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.

      Delete
    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.

      Delete
  2. 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.
    idprogramming.blogspot.ru/2011/06/how-to-get-user-data-from-active.html

    Regards,
    Ilya

    ReplyDelete
  3. does it works for SharePoint Online?

    ReplyDelete

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.