Active Directory with Javascript Examples
I’ll post up examples of Active Directory with Javascript as I go along.
How To Search For Users Example
function Search(search,SearchType) {
var arrSearchResult = [];
var strSearch = '';
switch(SearchType) {
case "contains":
strSearch = "*"+search+"*";
break;
case "begins":
strSearch = search+"*";
break;
case "ends":
strSearch = "*"+search;
break;
case "exact":
strSearch = search;
break;
default:
strSearch = "*"+search+"*";
break;
}
objRootDSE = GetObject("LDAP://RootDSE");
strDomain = objRootDSE.Get("DefaultNamingContext");
strOU = "OU=Users"; // Set the OU to search here.
strAttrib = "name,samaccountname"; // Set the attributes to retrieve here.
objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;
var Dom = "LDAP://"+strOU+","+strDomain;
var arrAttrib = strAttrib.split(",");
objCommand.CommandText = "select '"+strAttrib+"' from '"+Dom+"' WHERE objectCategory = 'user' AND objectClass='user' AND samaccountname='"+search+"' ORDER BY samaccountname ASC";
try {
objRecordSet = objCommand.Execute();
objRecordSet.Movefirst;
while(!(objRecordSet.EoF)) {
var locarray = new Array();
for(var y = 0; y < arrAttrib.length; y++) {
locarray.push(objRecordSet.Fields(y).value);
}
arrSearchResult.push(locarray);
objRecordSet.MoveNext;
}
return arrSearchResult;
} catch(e) {
alert(e.message);
}
}
Hi, your example is very good, i want know, if yuo can help me, i need agree an user at my Active Directory whit javascript, i want implement it in an App web, tanks and escuse my bad english :s
If you want to integrate a web app with Active Directory, a server side language would be more suitable.
No, because the server might be on another or unreachable domain.
Hi
if I use you code in my javascript, I have the error:
GetObject(“LDAP://RootDSE”) is not defined
Can you help me ? thank’s
It’s probably a better idea to use PowerShell rather than Javascript nowadays, however if you need to use javascript, make sure the script is being run on a computer that is connected to the domain that you’re trying to query
I have the same issue, not working in JavaScript. Did you figure out a solution
Does anyone know? If someone wanted to use your code and add the Active Directory (AD) username e-mail…etc to a static html form field how would they go about doing it?
I’m fussing with the same issue, GetObject line fails. Is it just not possible to do this in Javascript with Active Directory? I’m using it with an app that involves windows auth for a process, so figured there has to be some way to get that information.
The GetObject line relies on ActiveX. If you’re not using Internet explorer with the right(very insecure) security settings, it’s going to fail.
You have to use IE8 or older. You can force you browser to use IE8 by adding a line in the header of the html page.