Emil Nadinic

Hi Guys,

I've got a simple VB app I'm trying to convert into a sidebar gadget, but I'd like to know if this is even possible before spending too many hours trying to convert the code across.

The code simply connects to Active Directory and searches the Description fields, returng back any matches.

The way its currently being done in VB is as follows:

Code Snippet

Dim objRootDSE
Dim objCommand As ADODB.Command
Dim objConnection As ADODB.Connection

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(ObjectCategory=computer)"
strAttributes = "cn,distinguishedName,description"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

I then cycle through the recordset returning back any results.

Is something like this able to be converted into a sidebar gadget theoretically, or does the scripting environment not allow for these types of calls to be made



Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Andy E

I'm no expert, but from the looks of it, just using that code in VBScript would work. ADODB objects can be created with CreateObject and GetObject also works fine.

Andy




Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Emil Nadinic

I've managed to get it working on the rare occasion, but it seems to sit there most of the time doing absolutely nothing. Noobish question, but how on earth can I debug this

Code Snippet

Sub RunSub()

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(ObjectCategory=computer)(description=*" & ADSearchString.value & "*))"
strAttributes = "description"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

if objRecordSet.EOF then
msgbox "No records found"
else
msgbox objRecordSet.fields("description")
end if

End Sub





Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Andy E

Why not use MsgBox at intermittent points to give you an update on what's happening
I'm not really familiar with ADODB, so I couldn't tell you much more than that.

Andy




Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Jonathan Abbott

If this is in a Gadget, you first you want to recode it into Javascript. VB and Sidebar do not get on, you can get all sorts of obscure issues that cannot be explained.

It may be producing an error, so I've wrapped it with a try/catch to report what's going on. I've got no way of testing this, so I hope I've converted it without any errors:

Code Snippet

var objRootDSE = GetObject("LDAP://RootDSE");
// Determine DNS domain name.
var strDNSDomain = objRootDSE.Get("defaultNamingContext");
var objConnection = new ActiveXObject("ADODB.Connection");
var objCommand = new ActiveXObject("ADODB.Command");
var oShell = new ActiveXObject("WScript.Shell");


function RunSub() {
try{
objConnection.Provider = "ADsDSOObject";
objConnection.Open("Active Directory Provider");
objCommand.ActiveConnection = objConnection;

var strBase = "<LDAP://" + strDNSDomain + ">";
var strFilter = "(&(ObjectCategory=computer)(description=*" & ADSearchString.value & "*))";
var strAttributes = "description";
var strQuery = strBase + ";" + strFilter + ";" + strAttributes + ";subtree";

objCommand.CommandText = strQuery;
objCommand.Properties("Page Size") = 100;
objCommand.Properties("Timeout") = 30;
objCommand.Properties("Cache Results") = false;
var objRecordSet = objCommand.Execute;

if (objRecordSet.EOF) {
oShell.Popup("No records found");
} else {
oShell.Popup(objRecordSet.fields("description"));
}
} catch(err) {oShell.Popup(err.name+" - " + err.message)}
}








Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Emil Nadinic

Thanks guys - I tried putting msgbox's in various spots, and that worked to a degree, but some of the AD calls would fail with not return error message, which caught me for a while,

Apparently the description field needs to be treated as an array, not a string. I copied the code into a .vbs file and ran if from a command prompt (with the additional modifications) and got a "type mismatch" straight away. Fixed it back in the gadget and it works a treat now!

As for recoding it in javascript - concider it done - I'd prefer to have this thing in a state where I have at least some control over it

For the sake of completeness, the description code that works every time now looks like this:

Code Snippet

if objRecordSet.EOF then
Wscript.Echo "No records found"
else
do while not objRecordSet.EOF
arrDescription = objRecordSet.fields("description")
If IsNull(arrDescription) Then
Response = Response & "No Description"
Else
For Each strLine In arrDescription
Response = Response & "Description: " & strLine
Next
End If
objRecordSet.MoveNext
loop
end if

The next challenge is to work out how to represent this information on a flyout (instead of the message box) so I can copy and paste the information easily..





Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Emil Nadinic

Guys! Help!

I'm trying to work out how to represent the result of the query (which is typically a number of rows) on a flyout, and having great difficulty.

From a design point of view, would this be the best way to code up what I'm after

Goal: Gadget with a text field. Type something into the text field and a flyout come out with the resulting information.

Possible Approach:- Main Gadget contains a text field and nothing more. On submit, it calls a javascript function and passes the text submitted in the field to a javascript function. This javascript function shows the flyout, runs the Active Directory search, and returns the recordset as a table in the flyout.

I'm guessing this should be a fairly simply process, but I'm having difficulty working out where to run the relevant commands from. In the above code, the AD search was being called from the main gadget html, but this is looking less and less like the right place to run it from.

Guys, can I get some pointers as to where the right places are to run the various code segments





Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Andy E

In my gadgets, I usually call the searches from the main gadget window, then passed the search results to the Flyout window. It gives you more flexibility if you want to do more with the gadget window. If you set a global variable in your main gadget window, set the search results to it, then when your flyout opens you can use the following method in your flyout:

Code Snippet
set myVariable = System.Gadget.document.parentWindow.myVariable;


Assuming you set the search results to a global variable called myVariable, those results would now be available to the flyout window. Use your current function to show the flyout (maybe with an animated loading gif or just some loading text), then in the flyout body's onload event use the same method above to call the search function ie System.Gadget.document.parentWindow.searchFunction();

There's a few different ways you can do this and they're all good for different things. If you call the search function from the gadget window, those search results are kept even after the flyout window is closed. This means if a user inadvertently loses focus on the gadget and the flyout closes the search doesn't have to be performed again.

Andy






Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Emil Nadinic

Andy,

Thanks for your help! The gadget works perfectly (now that I understand what is supposed to run from where). My flyout appears with the Active Directory results, which I store in a DIV with overflow set to auto so I get scrollbars if needed.

The only thing I can't seem to do, which is wierd, is to copy and paste data out of the flyout. If I try to highlight it with the cursor, nothing highlights - is this normal behaviour for flyouts

My main function is constructing a table in a global variable which is being written out in the body of the flyout.





Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Jonathan Abbott

That's normal behaviour for Gadgets. You'll have to make it an editable text field.




Re: Sidebar Gadget Development Connecting to Active Directory within the Gadget

Emil Nadinic

Thanks Jomathan - I thought the behaviour was a little strange :-)

Found this article, going to give it a try:

http://www.htmlgoodies.com/beyond/javascript/article.php/3458851