Getting user's AD account

Discussion in 'Microsoft Outlook Program Addins' started by khai, Jan 16, 2009.

  1. khai

    khai Guest

    How can I get the user's AD account (i.e. username) via my Outlook plug-in in
    C#?
     
    khai, Jan 16, 2009
    #1
    1. Advertisements

  2. NameSpace.CurrentUser.AddressEntry returns an AddressEntry object. Use
    AddressEntry.Name or AddressEntry.Address to get whichever you want. Address
    will be in EX form as an EX DN.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm


    "khai" <> wrote in message
    news:...
    > How can I get the user's AD account (i.e. username) via my Outlook plug-in
    > in
    > C#?
     
    Ken Slovak - [MVP - Outlook], Jan 16, 2009
    #2
    1. Advertisements

  3. Outlook2007 only:
    Application.Session.CurrentUser.AddressEntry.GetExchangeUser.
    You can then use ExchangeUser.Alias property.

    --
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/
    OutlookSpy - Outlook, CDO
    and MAPI Developer Tool
    -
    "khai" <> wrote in message
    news:...
    > How can I get the user's AD account (i.e. username) via my Outlook plug-in
    > in
    > C#?
     
    Dmitry Streblechenko, Jan 16, 2009
    #3
  4. khai

    khai Guest

    This works for getting the sender's account. But how can I get the
    recipient's AD account?

    I have also noticed that in the Outlook address book, some of the user's
    email address (i.e. ... /cn=...) is not the same as the alias field. I need
    to get the user's AD account (i.e. login information) as I would like to
    programmatically assign the ACL based on the recipients list.

    "Ken Slovak - [MVP - Outlook]" wrote:

    > NameSpace.CurrentUser.AddressEntry returns an AddressEntry object. Use
    > AddressEntry.Name or AddressEntry.Address to get whichever you want. Address
    > will be in EX form as an EX DN.
    >
    > --
    > Ken Slovak
    > [MVP - Outlook]
    > http://www.slovaktech.com
    > Author: Professional Programming Outlook 2007.
    > Reminder Manager, Extended Reminders, Attachment Options.
    > http://www.slovaktech.com/products.htm
    >
    >
    > "khai" <> wrote in message
    > news:...
    > > How can I get the user's AD account (i.e. username) via my Outlook plug-in
    > > in
    > > C#?

    >
    >
     
    khai, Jan 19, 2009
    #4
  5. khai

    khai Guest

    Is there a workaround for Outlook 2003?

    "Dmitry Streblechenko" wrote:

    > Outlook2007 only:
    > Application.Session.CurrentUser.AddressEntry.GetExchangeUser.
    > You can then use ExchangeUser.Alias property.
    >
    > --
    > Dmitry Streblechenko (MVP)
    > http://www.dimastr.com/
    > OutlookSpy - Outlook, CDO
    > and MAPI Developer Tool
    > -
    > "khai" <> wrote in message
    > news:...
    > > How can I get the user's AD account (i.e. username) via my Outlook plug-in
    > > in
    > > C#?

    >
    >
    >
     
    khai, Jan 19, 2009
    #5
  6. To query the AD directly you would need to use an LDAP query, assuming you
    have permissions to run that. Take a look at
    http://www.outlookcode.com/threads.aspx?forumid=4&messageid=3870 and also
    search on other links at www.outlookcode.com on "LDAP" for examples of how
    to do that.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm


    "khai" <> wrote in message
    news:...
    > This works for getting the sender's account. But how can I get the
    > recipient's AD account?
    >
    > I have also noticed that in the Outlook address book, some of the user's
    > email address (i.e. ... /cn=...) is not the same as the alias field. I
    > need
    > to get the user's AD account (i.e. login information) as I would like to
    > programmatically assign the ACL based on the recipients list.
     
    Ken Slovak - [MVP - Outlook], Jan 19, 2009
    #6
  7. As Ken mentioned, you can either
    1. run an AD query,
    2. use Extended MAPI (C++ or Delphi) to read the PR_ACCOUNT property from
    the coresponding IMailUser object or
    3. <plug> use Redemption - it exposes RDOAddressEntry.Alias property and
    workds in all versions of Outlook </plug>.

    --
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/
    OutlookSpy - Outlook, CDO
    and MAPI Developer Tool
    -
    "khai" <> wrote in message
    news:D...
    > Is there a workaround for Outlook 2003?
    >
    > "Dmitry Streblechenko" wrote:
    >
    >> Outlook2007 only:
    >> Application.Session.CurrentUser.AddressEntry.GetExchangeUser.
    >> You can then use ExchangeUser.Alias property.
    >>
    >> --
    >> Dmitry Streblechenko (MVP)
    >> http://www.dimastr.com/
    >> OutlookSpy - Outlook, CDO
    >> and MAPI Developer Tool
    >> -
    >> "khai" <> wrote in message
    >> news:...
    >> > How can I get the user's AD account (i.e. username) via my Outlook
    >> > plug-in
    >> > in
    >> > C#?

    >>
    >>
    >>
     
    Dmitry Streblechenko, Jan 19, 2009
    #7
  8. khai

    khai Guest

    Are you referring to the following:
    '*** Get the current User
    Dim oWNetwork
    Set oWNetwork = CreateObject("WScript.Network")
    Set oRootDSE = GetObject("LDAP://RootDSE")
    Set oMyDomain = GetObject("LDAP://" & _
    oRootDSE.Get("defaultNamingContext"))
    Set cnn = CreateObject("ADODB.Connection")
    cnn.provider = "adsDSOObject"
    cnn.Open oMyDomain.ADSPath
    Set rst = CreateObject("ADODB.Recordset")

    '*** UserPath in AD
    StrSQL = "SELECT adspath " &_
    "FROM '" & oMyDomain.ADSPath & "'" & _
    "WHERE SAMAccountName ='" & oWNetwork.UserName & "'"
    rst.Open StrSQL, cnn
    ADUserPath = rst.Fields("adspath")
    Set ADAccount = GetObject(ADUserPath)

    '*** Users Data
    Item.UserProperties.Find("UserName") = ADAccount.FullName
    Item.UserProperties.Find("UserInitials") = ADAccount.initials
    Item.UserProperties.Find("userPhone") = ADAccount.telephoneNumber

    Is there a C# version?

    "Ken Slovak - [MVP - Outlook]" wrote:

    > To query the AD directly you would need to use an LDAP query, assuming you
    > have permissions to run that. Take a look at
    > http://www.outlookcode.com/threads.aspx?forumid=4&messageid=3870 and also
    > search on other links at www.outlookcode.com on "LDAP" for examples of how
    > to do that.
    >
    > --
    > Ken Slovak
    > [MVP - Outlook]
    > http://www.slovaktech.com
    > Author: Professional Programming Outlook 2007.
    > Reminder Manager, Extended Reminders, Attachment Options.
    > http://www.slovaktech.com/products.htm
    >
    >
    > "khai" <> wrote in message
    > news:...
    > > This works for getting the sender's account. But how can I get the
    > > recipient's AD account?
    > >
    > > I have also noticed that in the Outlook address book, some of the user's
    > > email address (i.e. ... /cn=...) is not the same as the alias field. I
    > > need
    > > to get the user's AD account (i.e. login information) as I would like to
    > > programmatically assign the ACL based on the recipients list.

    >
    >
     
    khai, Jan 20, 2009
    #8
  9. Yes, that's the sort of LDAP query you'd need. Google is your friend for
    trying to find a C# example of an LDAP query.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm


    "khai" <> wrote in message
    news:...
    > Are you referring to the following:
    > '*** Get the current User
    > Dim oWNetwork
    > Set oWNetwork = CreateObject("WScript.Network")
    > Set oRootDSE = GetObject("LDAP://RootDSE")
    > Set oMyDomain = GetObject("LDAP://" & _
    > oRootDSE.Get("defaultNamingContext"))
    > Set cnn = CreateObject("ADODB.Connection")
    > cnn.provider = "adsDSOObject"
    > cnn.Open oMyDomain.ADSPath
    > Set rst = CreateObject("ADODB.Recordset")
    >
    > '*** UserPath in AD
    > StrSQL = "SELECT adspath " &_
    > "FROM '" & oMyDomain.ADSPath & "'" & _
    > "WHERE SAMAccountName ='" & oWNetwork.UserName & "'"
    > rst.Open StrSQL, cnn
    > ADUserPath = rst.Fields("adspath")
    > Set ADAccount = GetObject(ADUserPath)
    >
    > '*** Users Data
    > Item.UserProperties.Find("UserName") = ADAccount.FullName
    > Item.UserProperties.Find("UserInitials") = ADAccount.initials
    > Item.UserProperties.Find("userPhone") = ADAccount.telephoneNumber
    >
    > Is there a C# version?
    >
    > "Ken Slovak - [MVP - Outlook]" wrote:
    >
    >> To query the AD directly you would need to use an LDAP query, assuming
    >> you
    >> have permissions to run that. Take a look at
    >> http://www.outlookcode.com/threads.aspx?forumid=4&messageid=3870 and also
    >> search on other links at www.outlookcode.com on "LDAP" for examples of
    >> how
    >> to do that.
    >>
    >> --
    >> Ken Slovak
    >> [MVP - Outlook]
    >> http://www.slovaktech.com
    >> Author: Professional Programming Outlook 2007.
    >> Reminder Manager, Extended Reminders, Attachment Options.
    >> http://www.slovaktech.com/products.htm
    >>
    >>
    >> "khai" <> wrote in message
    >> news:...
    >> > This works for getting the sender's account. But how can I get the
    >> > recipient's AD account?
    >> >
    >> > I have also noticed that in the Outlook address book, some of the
    >> > user's
    >> > email address (i.e. ... /cn=...) is not the same as the alias field. I
    >> > need
    >> > to get the user's AD account (i.e. login information) as I would like
    >> > to
    >> > programmatically assign the ACL based on the recipients list.

    >>
    >>
     
    Ken Slovak - [MVP - Outlook], Jan 20, 2009
    #9
  10. khai

    khai Guest

    I found a workaround:

    ##############################
    Domain dmn = Domain.GetCurrentDomain();
    DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Domain,
    dmn.Name);
    DomainControllerCollection dcc = DomainController.FindAll(ctx);

    foreach(DomainController dc in dcc)
    {
    DirectorySearcher ds;
    using (ds = dc.GetDirectorySearcher())
    {
    ds.Filter = String.Format("(email=" + emailAcc + ")");
    }
    SearchResult sr = ds.FindOne();
    ResultPropertyCollection rpc = sr.Properties;
    foreach(string prop in rpc.PropertyNames)
    {
    foreach(Object obj in rpc[prop])
    {
    if (prop.ToLower() == "mailnickname")
    {
    userName = obj.ToString();
    }
    }
    }
    }
    ....
    #################################

    This works on my development environment. However when I port this over to
    my production environment, I receive the following error:

    "The server is not operational"

    @ line 'SearchResult sr = ds.FindOne();'

    I tried with FindAll(), but receive the same error.

    Is there an explanation or solution for this?


    "Ken Slovak - [MVP - Outlook]" wrote:

    > Yes, that's the sort of LDAP query you'd need. Google is your friend for
    > trying to find a C# example of an LDAP query.
    >
    > --
    > Ken Slovak
    > [MVP - Outlook]
    > http://www.slovaktech.com
    > Author: Professional Programming Outlook 2007.
    > Reminder Manager, Extended Reminders, Attachment Options.
    > http://www.slovaktech.com/products.htm
    >
    >
    > "khai" <> wrote in message
    > news:...
    > > Are you referring to the following:
    > > '*** Get the current User
    > > Dim oWNetwork
    > > Set oWNetwork = CreateObject("WScript.Network")
    > > Set oRootDSE = GetObject("LDAP://RootDSE")
    > > Set oMyDomain = GetObject("LDAP://" & _
    > > oRootDSE.Get("defaultNamingContext"))
    > > Set cnn = CreateObject("ADODB.Connection")
    > > cnn.provider = "adsDSOObject"
    > > cnn.Open oMyDomain.ADSPath
    > > Set rst = CreateObject("ADODB.Recordset")
    > >
    > > '*** UserPath in AD
    > > StrSQL = "SELECT adspath " &_
    > > "FROM '" & oMyDomain.ADSPath & "'" & _
    > > "WHERE SAMAccountName ='" & oWNetwork.UserName & "'"
    > > rst.Open StrSQL, cnn
    > > ADUserPath = rst.Fields("adspath")
    > > Set ADAccount = GetObject(ADUserPath)
    > >
    > > '*** Users Data
    > > Item.UserProperties.Find("UserName") = ADAccount.FullName
    > > Item.UserProperties.Find("UserInitials") = ADAccount.initials
    > > Item.UserProperties.Find("userPhone") = ADAccount.telephoneNumber
    > >
    > > Is there a C# version?
    > >
    > > "Ken Slovak - [MVP - Outlook]" wrote:
    > >
    > >> To query the AD directly you would need to use an LDAP query, assuming
    > >> you
    > >> have permissions to run that. Take a look at
    > >> http://www.outlookcode.com/threads.aspx?forumid=4&messageid=3870 and also
    > >> search on other links at www.outlookcode.com on "LDAP" for examples of
    > >> how
    > >> to do that.
    > >>
    > >> --
    > >> Ken Slovak
    > >> [MVP - Outlook]
    > >> http://www.slovaktech.com
    > >> Author: Professional Programming Outlook 2007.
    > >> Reminder Manager, Extended Reminders, Attachment Options.
    > >> http://www.slovaktech.com/products.htm
    > >>
    > >>
    > >> "khai" <> wrote in message
    > >> news:...
    > >> > This works for getting the sender's account. But how can I get the
    > >> > recipient's AD account?
    > >> >
    > >> > I have also noticed that in the Outlook address book, some of the
    > >> > user's
    > >> > email address (i.e. ... /cn=...) is not the same as the alias field. I
    > >> > need
    > >> > to get the user's AD account (i.e. login information) as I would like
    > >> > to
    > >> > programmatically assign the ACL based on the recipients list.
    > >>
    > >>

    >
    >
     
    khai, Jan 21, 2009
    #10
  11. I don't know. I don't do a lot with LDAP queries. You might be best off
    posting this in one of the Exchange development groups where they use LDAP
    more often, such as microsoft.public.exchange.applications.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm


    "khai" <> wrote in message
    news:...
    >I found a workaround:
    >
    > ##############################
    > Domain dmn = Domain.GetCurrentDomain();
    > DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Domain,
    > dmn.Name);
    > DomainControllerCollection dcc = DomainController.FindAll(ctx);
    >
    > foreach(DomainController dc in dcc)
    > {
    > DirectorySearcher ds;
    > using (ds = dc.GetDirectorySearcher())
    > {
    > ds.Filter = String.Format("(email=" + emailAcc + ")");
    > }
    > SearchResult sr = ds.FindOne();
    > ResultPropertyCollection rpc = sr.Properties;
    > foreach(string prop in rpc.PropertyNames)
    > {
    > foreach(Object obj in rpc[prop])
    > {
    > if (prop.ToLower() == "mailnickname")
    > {
    > userName = obj.ToString();
    > }
    > }
    > }
    > }
    > ...
    > #################################
    >
    > This works on my development environment. However when I port this over to
    > my production environment, I receive the following error:
    >
    > "The server is not operational"
    >
    > @ line 'SearchResult sr = ds.FindOne();'
    >
    > I tried with FindAll(), but receive the same error.
    >
    > Is there an explanation or solution for this?
     
    Ken Slovak - [MVP - Outlook], Jan 21, 2009
    #11
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mihail Edkov

    Again about getting sender address when send mail ?

    Mihail Edkov, Jul 8, 2003, in forum: Microsoft Outlook Program Addins
    Replies:
    2
    Views:
    1,349
    Mihail Edkov
    Jul 9, 2003
  2. Mikhail

    problem getting the SMTP address (Outlook 2003)

    Mikhail, Oct 27, 2003, in forum: Microsoft Outlook Program Addins
    Replies:
    1
    Views:
    724
    Ken Slovak - [MVP - Outlook]
    Oct 29, 2003
  3. David

    getting folder change notifications in C++

    David, Nov 4, 2003, in forum: Microsoft Outlook Program Addins
    Replies:
    1
    Views:
    555
    Dmitry Streblechenko
    Nov 5, 2003
  4. Nikolay S

    The OutlookApplication Getting fail

    Nikolay S, Nov 12, 2003, in forum: Microsoft Outlook Program Addins
    Replies:
    0
    Views:
    489
    Nikolay S
    Nov 12, 2003
  5. Guest

    Adding more email account to my outlook account.

    Guest, Jan 11, 2006, in forum: Microsoft Outlook Program Addins
    Replies:
    0
    Views:
    304
    Guest
    Jan 11, 2006
Loading...

Share This Page