title: Get Active Directory ExtensionAttribute via UserPrincipal
body: <p>I am trying to get the value of user's <code>ExtensionAttribute4</code>.</p>
  
  <p>This is my extension class to <code>UserPrincipal</code>:</p>
  
  <pre><code>[DirectoryRdnPrefix("CN")]
  [DirectoryObjectClass("Person")]
  public class UserPrincipalEx : UserPrincipal
  {
      // Implement the constructor using the base class constructor. 
      public UserPrincipalEx(PrincipalContext context) : base(context)
      { }
  
      // Implement the constructor with initialization parameters.    
      public UserPrincipalEx(PrincipalContext context,
                           string samAccountName,
                           string password,
                           bool enabled) : base(context, samAccountName, password, enabled)
      { }
  
      // Create the "extensionAttribute4" property.    
      [DirectoryProperty("extensionAttribute4")]
      public string ExtensionAttribute4
      {
          get
          {
              if (ExtensionGet("extensionAttribute4").Length != 1)
                  return string.Empty;
  
              return (string)ExtensionGet("extensionAttribute4")[0];
          }
          set { ExtensionSet("extensionAttribute4", value); }
      }
  
      public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
      {
          return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
      }
  
      // Implement the overloaded search method FindByIdentity. 
      public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
      {
          return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
      }
  }
  </code></pre>
  
  <p>Here is how I call the method:  </p>
  
  <pre><code> UserPrincipalEx user = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");
  </code></pre>
  
  <p>I see in debug that user really is result of <code>Classes.UserPrincipalEx.FindByIdentity</code>, but <code>extensionAttribute4</code> is not listed there. At all. Other attributes are there.</p>
  
  <p>I tried to do the same with Manager, same result. The error is:  </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>I am new to this, so please excuse me if I am missing something obvious. </p>