locked
Cannot write to DirectoryEntry property if property is null RRS feed

  • Question

  • User100615095 posted

    Hi,

    I am working with an intranet which links users to AD via the SAMAccountName attribute.

    I am able to update properties which are already populated in AD. However, when I try to write to properties which are null, I get the following error:

    "The attribute syntax specified to the directory service is invalid"

    An example of one of the properties where I get this error is streetAddress. If I subsequently ask someone in the network admin team to put a value into the streetAddress field, then I can overwrite this without a problem.

    The method I am calling is as follows:

            private Boolean WriteToAD(DirectoryEntry record, String adPropertyName, String newValue)
            {
                if (String.IsNullOrEmpty(adPropertyName) || String .IsNullOrEmpty(newValue)) return false;

                if (record.Properties.Contains(adPropertyName))
                {
                    record.Properties[adPropertyName][0] = newValue;
                }
                else
                {
                    record.Properties[adPropertyName].Add(newValue);
                }
                try
                {
                    record.CommitChanges();
                }
                catch (Exception ex)
                {

                    throw new Exception("Error writing to AD with: "+ newValue + " to " + propertyName + ex.Message);

                }
                return record.Properties[adPropertyName].Value.ToString() == newValue;
            }

      The DirectoryEntry record object has its AuthenticationType set to AuthenticationTypes.Secure and UserName and Password set to a user that has permission to write to AD.

    Regards,

    Pete

    Monday, September 26, 2011 10:38 AM

Answers

  • User1560780540 posted

    This method looks fine to me. I tried it and it is working fine.

    As per the error, the syntax of the value you are specifying for the adproperty is invalid. Did you try the code with some other properties? what is the value you are specifying for street address?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 27, 2011 12:57 AM

All replies

  • User1560780540 posted

    This method looks fine to me. I tried it and it is working fine.

    As per the error, the syntax of the value you are specifying for the adproperty is invalid. Did you try the code with some other properties? what is the value you are specifying for street address?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 27, 2011 12:57 AM
  • User-625738215 posted

    Dear All i am updating Department , Title , Mobile no , contry , all feilds are updating but when i send Manager feilds for updating but this feilds not update please help me how i update manager feilds of Active Diractory  through vb.net asp.net

    Thursday, December 1, 2011 9:06 AM
  • User636753033 posted

    You should just need to make sure the string is in the format of the user distinquishedname

    Thursday, December 1, 2011 2:09 PM
  • User-625738215 posted

    i am using this formate string

    CN=Muhammad Javed/IT/Lahore,OU=Information Technology,DC=WTPK,DC=local

    And

    CN=Jamal.Yaqub/IT/Lahore,OU=Members,OU=Information Technology,OU=Central,OU=Wateen,DC=WTPK,DC=local

    but still getting error

     

     

    Friday, December 2, 2011 12:31 AM
  • User1560780540 posted

    The distinuishedName contains special chars "/". you should escape this char.

    Please review this article :

    http://www.rlmueller.net/CharactersEscaped.htm

     

    Change the distinguidhedName

    CN=Muhammad Javed/IT/Lahore,OU=Information Technology,DC=WTPK,DC=local

    with

    CN=Muhammad Javed\/IT\/Lahore,OU=Information Technology,DC=WTPK,DC=local

     

     

    Friday, December 2, 2011 1:24 AM
  • User-625738215 posted

    Thaks kushal.dwivedi

    kushal.dwivedi

     

    ,

     

    Friday, December 2, 2011 4:22 AM
  • User-625738215 posted

    Dear All can you tell me how i can find OU in active Diractory help me to find OU list from active Diractory property

    Friday, December 2, 2011 4:27 AM
  • User636753033 posted

    i am using this formate string

    CN=Muhammad Javed/IT/Lahore,OU=Information Technology,DC=WTPK,DC=local

    And

    CN=Jamal.Yaqub/IT/Lahore,OU=Members,OU=Information Technology,OU=Central,OU=Wateen,DC=WTPK,DC=local

    but still getting error

    Best way to handle this is not to try and enter it manually since the distinguished name can contain many OUs to the location where the user object is. Create a function that will search the directory using the sameaccountname of the user and return his distinguishedname as a string. No need to worry about escape characters or formatting then.

    Sunday, December 4, 2011 8:59 AM
  • User636753033 posted

    Dear All can you tell me how i can find OU in active Diractory help me to find OU list from active Diractory property

    See this post, http://forums.asp.net/t/945728.aspx/1

    Sunday, December 4, 2011 9:01 AM