Wednesday 6 January 2016

Read AD user group information from X++

static void readCompanyInfofromUserGroupWorking(Args _args)
 {
 System.DirectoryServices.DirectorySearcher directorySearcher;
 System.DirectoryServices.SearchScope searchScope;
 System.DirectoryServices.DirectoryEntry directoryEntry;
 System.DirectoryServices.SearchResultCollection searchResultCollection;
 System.DirectoryServices.SearchResult searchResult;
 System.DirectoryServices.PropertyCollection propertyCollection;
 System.DirectoryServices.PropertyValueCollection propertyValueCollection;

 str networkDomain="ax2012dev.local";

str prefix = 'LDAP://';

int totalCount;

int counter;

str groupName="AccountRecievableNew";

str groupCrit;

int usercount;

int ucount;

str userinfo;



 try


 {
 directoryEntry = new System.DirectoryServices.DirectoryEntry(prefix + networkDomain);

 searchScope = CLRInterop::parseClrEnum('System.DirectoryServices.SearchScope', 'Subtree');



 directorySearcher = new System.DirectoryServices.DirectorySearcher(DirectoryEntry);


 directorySearcher.set_SearchScope(searchScope);

 groupCrit = strfmt('(samaccountname=%1)', groupName) ;

 directorySearcher.set_Filter(strfmt('(&(objectClass=group)%1)', groupCrit));



 searchResultCollection = directorySearcher.FindAll();

 totalCount = SearchResultCollection.get_Count();

 for (counter=0; counter < totalcount; counter++)


 {
 searchResult = searchResultCollection.get_Item(counter);
 directoryEntry = searchResult.GetDirectoryEntry();
 if (directoryEntry)


 {
 propertyCollection = directoryEntry.get_Properties();
 if (propertyCollection)


 {
 propertyValueCollection = propertyCollection.get_Item('Info');

if (propertyValueCollection)


 {
 if (propertyValueCollection.get_Value())


 {
 info(propertyValueCollection.get_Value());
 }
 }
 }
 }
 }

 directorySearcher.Dispose();
 searchResultCollection.Dispose();
 }

 catch (Exception::CLRError)


 {
 error("Error reading AD");

return;


 }
 }
 

No comments:

Post a Comment