I have an OpenQuery call to Active Directory that gets the email address for a given Windows Domain account name. It works fine until I put it in an Insert trigger. The offending piece of T-SQL is:
set @sql = 'select mail from openquery(ADSI, '''
+ 'SELECT mail FROM ''''LDAP://DC=COMPANY,DC=LOCAL''''
WHERE objectClass=''''user'''' AND objectClass<>''''computer''''
AND samAccountName=''''username'''''') '
exec sp_executesql @sql, N'@recipients varchar(500) output', @recipients output
ADSI is a linked server and I have the security context set to always use a specific user account. I get the following error returned in my web app when adding a new record.
"The member with identity 'mail' does not exist in the metadata collection. Parameter name: identity"
I have tried connecting as the same user that my app uses for connecting to the database and running the query from SQL Management Studio and I cannot replicate the problem outside of the Trigger. Can someone point me in the right direction? I suspect it is a security issue but I am lost and running out of ideas.