Implementing Join/Leave link (part 2): Join/Leave a Group


Continuing on from my initial post I want to cover the bit of code needed to join or leave a group.  As before a reference to and using statement for System.Directory services is required, but in this instance Exchange Web Services (EWS) is not used and no references for it are necessary.


Again, this is code that I’m putting together from other source files to show in the blog, but isn’t meant as entire piece of compilable source.  I’ll assume for the purpose of this post that the user email nick and the Distribution Group (DG) email nick are passed in to variables UserNick and DGNick respectively.


Assuming that I couldn’t easily get the Distinguished Name of the user I will have to grab the user object and use it to grab the DN.  Finding the objects (in this case group and user) is accomplished by using the DirectorySearcher object and sending in a query string matching a particular format.  In this case the query string is: (&(mailnickname=[DGNick])(|(objectCategory=group)))


I really have no idea about the syntax or its derivation.  Outside of the use of the pipe and the ampersand it is pretty clear, but I have no idea why those pieces are needed.  Once the query has been issued a list of results will be returned.  Since the mail nickname must be unique it can be assumed to be element 0 of the results collection if it exists.  The code is as follows:


//instantiate and initialize the DirectorySearcher


DirectorySearcher dirSearcher = new DirectorySearcher(“(&(mailnickname=” + DGNick + “)(|(objectCategory=group)))”);


//Execute the FindAll method


SearchResultCollection results = dirSearcher.FindAll();


//grab the first result if it isn’t null


DirectoryEntry grp = results[0] != null ? results[0].GetDirectoryEntry() : null;



Once that has been accomplished the user object must be fetched similarly:


//reset the DirectorySearcher Filter


dirSearcher.Filter = “(&(mailnickname=” + UserNick + “)(|(objectCategory=user)))”;


//Execute FindAll


SearchResultCollection userResults = dirSearcher.FindAll();


//grab the entry same as before


DirectoryEntry user = results[0] != null ? userResults[0].GetDirectoryEntry() : null;



With both objects in-hand the action to join or leave may be taken.  In this case I’ll assume that a string was passed in indicating whether to join or leave the group.  Once the object action has been taken the changes must then be committed by a separate call:


if (Action == “Leave”)


{


grp.Properties[“member”].Remove(user.Properties[“DistinguishedName”].Value);


grp.CommitChanges();


}


else if (Action == “Join”)


{


grp.Properties[“member”].Add(user.Properties[“DistinguishedName”].Value);


grp.CommitChanges();


}


 


That pretty much wraps up the join/leave functionality.  For those not familiar with how the AD-Exchange relationship works the users will be joined or removed from the DG in Exchange via the changes that are committed via AD.  The next post will be on the implementation of this code within a RESTful service.

Comments (1)

Skip to main content