Problem Description:
Fetch current login user data from active directory using JavaScript in SharePoint.
Resolution:
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...
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.
ReplyDeletecould you please give some brief details on the same?
DeleteIs 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
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.
DeleteUsually 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.
-Go to internet options.
Delete-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.
Hello.
ReplyDeleteWhy 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
does it works for SharePoint Online?
ReplyDelete