Tuesday, November 17, 2009

Reusing the SSP LDAP Connection

So, I was asked to generate a new SPList in MOSS 2007 based on a specific AD group. And, as I brainstormed, I recalled an AD connection setup within SSP to import user profiles. "Now, wouldn't it be nice to leverage the SSP LDAP connection to setup my LDAP connection," I thought. Well, it would be nice indeed and would take away the headache of having to setup a new webconfig item for my LDAP.
And thus, I scoured the web for articles that focused in on this technique for grabbing LDAP connectivity, but my search turned cold pretty quick. Since I'm so hard-headed, I decided I'd uncover this gem myself and here is what I discovered.

By getting an instance of the UserProfileConfigManager, I immediately had access to the available datasources in SSP. With a UserProfiles.Datasource in hand, I enumerated the connections and looped through until I found the one I wanted. With that, I now had the information I needed to build my LDAP connection. Check it out...

------------------------------------------------------------------
string ldapConnectionPath = string.Empty;
UserProfileConfigManager configMgr = new UserProfileConfigManager(ServerContext.Current);
DataSource adDataSource = configMgr.GetDataSource();

System.Collections.IEnumerator ConnEnumerator = adDataSource.Connections.GetEnumerator();
ConnEnumerator.Reset();

while (ConnEnumerator.MoveNext())
{
if(ConnEnumerator.Current.GetType().Name.Equals("LDAPConnection"))
{
LDAPConnection adConn = (LDAPConnection)ConnEnumerator.Current;
ldapConnectionPath = string.format("LDAP://{0}:{1}/{2}",
adConn.Server,
adConn.Port,
adConn.SearchBase);

// adConn.DomainName // Domain Name is also available
break;
}
}

No comments:

Post a Comment