How to get exchange server address from C# Add-in?

P

Piyush Gupta

Hi All,

I am developing add-in for Outlook 2003 (for Exchange Server 2003)
using VSTO/C# 2005.

As I found during my research on Outlook object model, it doesn't
expose active directory security groups for the current user. To
determine whether current user belongs to a specific active directory
security group or not, I am executing LDAP query to get all security
groups belonging to the user.

Can you pls. point me the property/method to get exchange server
address where I can fire this LDAP query? It'll be great if you can
suggest another way to get security groups for the current user.

Thanks,
Piyush.
 
D

Dmitry Streblechenko

Here is what I use to get the Exchange server name given a user name
(Delphi). Note that most likely than not the AD server name will be
different from that of the Exchange server.

function HrGetExchangeServerName(UserName : string):string;
var strCurrUserName, strCurrDomainName : string;
ADSIUser : IADsUser;
ADsNameTranslate : IADsNameTranslate;
DN : string;
Flags : integer;
res : HResult;
p : integer;
begin
p:=Pos('\', UserName);
if p > 0 then begin
strCurrUserName:=Copy(UserName, p+1, MaxInt);
strCurrDomainName:=Copy(UserName, 1, p-1);
end
else begin
if not HrGetUserNameAndDomain(strCurrUserName, strCurrDomainName) then
raise EOleSysError.Create('Culd not retrieve current user''s network name
and domain', E_UNEXPECTED, 0);
end;
ADsNameTranslate:=CreateOleObject('NameTranslate') as IADsNameTranslate;
//DNL
ADsNameTranslate.Init(ADS_NAME_INITTYPE_DOMAIN, strCurrDomainName);
if Pos('\', UserName) > 0 then Flags:=ADS_NAME_TYPE_NT4
else if Pos('@', UserName) > 0 then Flags:=ADS_NAME_TYPE_DOMAIN_SIMPLE
else Flags:=ADS_NAME_TYPE_UNKNOWN;
ADsNameTranslate.Set_(Flags, UserName);
DN:=ADsNameTranslate.Get(ADS_NAME_TYPE_1779);
//find the LDAP object from the AD
res:=ADsGetObject(PWideChar(WideString('LDAP://' + strCurrDomainName + '/'
+ DN)), IID_IADsUser, pointer(ADSIUser));
CheckRaiseMAPIError(res, 'ADsGetObject');
ADSIUser.GetInfoEx(VarArrayOf(['msExchHomeServerName']), 0);
Result:=ADSIUser.Get('msExchHomeServerName');
p:=PosFromEnd('=', Result);
if p > 0 then Result:=Copy(Result, p+1, MaxInt);
end;

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top