Novell.Directory.Ldap.LdapConnection.GetSchemaDN C# (CSharp) Method

GetSchemaDN() public method

Retrieves the Distiguished Name (DN) of the schema associated with a entry in the Directory. The DN can be used with the methods fetchSchema and modify to retreive and extend schema definitions. Reads the subschemaSubentry of the entry specified.
LDAPException This exception occurs if a null or empty /// value is passed as dn, if the subschemasubentry attribute cannot /// be retrieved, or the subschemasubentry contains multiple values. /// ///
public GetSchemaDN ( System dn ) : System.String
dn System Distinguished Name of any entry. The subschemaSubentry /// attribute is queried from this entry. /// ///
return System.String
        public virtual System.String GetSchemaDN(System.String dn)
        {
            System.String[] attrSubSchema = new System.String[]{"subschemaSubentry"};

            /* Read the entries subschemaSubentry attribute. Throws an exception if
            * no entries are returned. */
            LdapEntry ent = this.Read(dn, attrSubSchema);

            LdapAttribute attr = ent.getAttribute(attrSubSchema[0]);
            System.String[] values = attr.StringValueArray;
            if (values == null || values.Length < 1)
            {
                throw new LdapLocalException(ExceptionMessages.NO_SCHEMA, new System.Object[]{dn}, LdapException.NO_RESULTS_RETURNED);
            }
            else if (values.Length > 1)
            {
                throw new LdapLocalException(ExceptionMessages.MULTIPLE_SCHEMA, new System.Object[]{dn}, LdapException.CONSTRAINT_VIOLATION);
            }
            return values[0];
        }

Same methods

LdapConnection::GetSchemaDN ( ) : System.String

Usage Example

 static void Main(string[] args)
 {
     if ( args.Length != 6)
     {
         Console.WriteLine("Usage:   mono GetAttributeSchema <host name> <ldap port>  <login dn>" + " <password> <search base>" + " <search filter>");
         Console.WriteLine("Example: mono GetAttributeSchema Acme.com 389"  + " \"cn=admin,o=Acme\"" + " secret \"ou=sales,o=Acme\"" + "         \"(objectclass=*)\"");
         return;
     }
     string ldapHost = args[0];
     int ldapPort = System.Convert.ToInt32(args[1]);
     String loginDN  = args[2];
     String password = args[3];
     String searchBase = args[4];
     String searchFilter = args[5];
     try
     {
         LdapConnection conn= new LdapConnection();
         Console.WriteLine("Connecting to:" + ldapHost);
         conn.Connect(ldapHost,ldapPort);
         conn.Bind(loginDN,password);
         LdapSchema dirschema=conn.FetchSchema(conn.GetSchemaDN());
         LdapSearchResults lsc=conn.Search(  searchBase,
             LdapConnection.SCOPE_ONE,
             searchFilter,
             null,
             false);
         while (lsc.hasMore())
         {
             LdapEntry nextEntry = null;
             try
             {
                 nextEntry = lsc.next();
             }
             catch(LdapException e)
             {
                 Console.WriteLine("Error: " + e.LdapErrorMessage);
                 // Exception is thrown, go for next entry
                 continue;
             }
             Console.WriteLine("\n\n\n");
             Console.WriteLine("\n" + nextEntry.DN);
             LdapAttributeSet attributeSet = nextEntry.getAttributeSet();
             System.Collections.IEnumerator ienum=attributeSet.GetEnumerator();
             while(ienum.MoveNext())
             {
                 LdapAttribute attribute=(LdapAttribute)ienum.Current;
                 string attributeName = attribute.Name;
                 Console.WriteLine(  attributeName + ":  " + dirschema.getAttributeSchema(attributeName).ToString());
             }
         }
         conn.Disconnect();
     }
     catch(LdapException e)
     {
         Console.WriteLine("Error:" + e.LdapErrorMessage);
         return;
     }
     catch(Exception e)
     {
         Console.WriteLine("Error:" + e.Message);
         return;
     }
 }