DirectoryServices and getting/setting custom Propertyhttp://forums.asp.net/t/1794547.aspx/1?DirectoryServices+and+getting+setting+custom+PropertyThu, 19 Apr 2012 17:34:04 -040017945474940387http://forums.asp.net/p/1794547/4940387.aspx/1?DirectoryServices+and+getting+setting+custom+PropertyDirectoryServices and getting/setting custom Property <p>Hi&nbsp;</p> <p>I am trying to add a new custom attribute in the driectory entry. When i am committing chnages to it. it gives me error &quot;The specified directory service attribute or value does not exis&quot;</p> <p></p> <p>I have created a windows service which is accessign the AD.</p> <p>When there are no new attributes added then no error comes but when a new attribute is added the error above comes.</p> <p>for setting teh value of teh attribue i am using follwoing function&nbsp;</p> <p>Public Function SetAttribute(ByVal oEntry As DirectoryEntry, ByVal sAttributeName As String, ByVal objValue As Object) As Boolean</p> <p>If oEntry.Properties.Contains(sAttributeName) Then<br> Try<br> oEntry.Properties(sAttributeName).Value = objValue<br> bReturn = True</p> <p>Catch ex As Exception<br> EventLogHelper.LogFailureAudit(&quot;Error Setting Attribute &quot; &#43; sAttributeName &#43; &quot; &quot; &#43; ex.Message &#43; Environment.NewLine &#43; Environment.NewLine &#43; ex.StackTrace)<br> End Try</p> <p>END if</p> <p>End sub</p> <p></p> <p><span style="text-decoration:underline">My path of the directory entry is set like this:</span></p> <p></p> <p>Protected Function Z_CreateDirectoryEntry(ByVal sOU As String) As DirectoryEntry</p> <p>Dim oDirectoryEntry As DirectoryEntry = Nothing<br> Dim sPath As System.Text.StringBuilder = New System.Text.StringBuilder(&quot;&quot;)</p> <p>If m_sServer.Length &lt;&gt; 0 And m_sDomain.Length &lt;&gt; 0 Then<br> sPath.AppendFormat(&quot;LDAP://{0}/&quot;, m_sServer)<br> If sOU.Length &lt;&gt; 0 Then<br> sPath.Append(sOU)<br> sPath.AppendFormat(&quot;,{0}&quot;, m_sDomain)<br> Else<br> sPath.Append(m_sDomain)<br> End If<br> End If</p> <p>If sPath.Length &lt;&gt; 0 Then<br> If m_sUserName &lt;&gt; &quot;&quot; Then<br> oDirectoryEntry = New DirectoryEntry(sPath.ToString, m_sUserName, m_sPassword)<br> Else<br> oDirectoryEntry = New DirectoryEntry(sPath)<br> End If</p> <p>If m_iAuthenticationType &lt;&gt; -1 Then<br> oDirectoryEntry.AuthenticationType = m_iAuthenticationType<br> End If</p> <p>End If</p> <p>Return oDirectoryEntry</p> <p>End Function</p> <p>The path comes something like this</p> <p></p> <p>&quot;LDAP://ipaddress of Server/DC=DEV,DC=COM&quot;</p> <p></p> <p>Authetication type is <strong>delgations</strong></p> <p></p> <p>Please assist where it si going wrong.&nbsp;</p> 2012-04-19T07:29:20-04:004941570http://forums.asp.net/p/1794547/4941570.aspx/1?Re+DirectoryServices+and+getting+setting+custom+PropertyRe: DirectoryServices and getting/setting custom Property <p>If the propery is not populated on the object then it will not exist. So in the following code you have it check for that property before trying to change it. But if it currenlty does not have a value then the code will skip trying to modify it.</p> <pre class="prettyprint">If oEntry.Properties.Contains(sAttributeName) Then '.... End If</pre> <p></p> <p>If it does not matter if the property is already populated then remove Properties.Contains If/Else amd just set the value. You may also want to check to make sure you are using the correct property name. If you have access to ADUC, browse to an object of the same type (user or computer) and view its properties. There will be an Attribute Editor tab listing all properties the object has, populated or not. And you can check the name of the attribute there. You must browse for the obect, if you try to search for it in ADUC that tab will not show.</p> 2012-04-19T17:34:04-04:00