Subscribe RSS Join our Facebook Group Follow us on Twitter!
in Search

Microsoft and You

Get Groups of a User in AD and Retrieving Sharepoint List Item Collection

How to Get Groups of a User in Active Directory

Berikut ini snippet code C# untuk mendapatkan Active Directory Group dari username-nya.

   1: public List<string> GetGroupsMemberOF(string UserName)
   2:         {
   3:             List<string> Roles = new List<string>();
   4:             DirectoryEntry Entry = new DirectoryEntry("LDAP://dyware-research.com");
   5:             DirectorySearcher Search = new DirectorySearcher(Entry);
   6:             Search.Filter = String.Format("(&(objectCategory=person)(samAccountName={0}))", UserName);
   7:             Search.PropertiesToLoad.Add("memberOf");
   8:             SearchResult Result = Search.FindOne();
   9:             if (Result != null)
  10:             {
  11:                 int i = 0;
  12:                 foreach (var item in Result.Properties["memberOf"])
  13:                 {
  14:                     string[] RoleDetails = Result.Properties["memberOf"][i].ToString().Split(',');
  15:                     if (RoleDetails.Length > 0)
  16:                     {
  17:                         if (RoleDetails[0].IndexOf("CN=") != -1)
  18:                             RoleDetails[0] = RoleDetails[0].Replace("CN=", "");
  19:                         Roles.Add(RoleDetails[0]);
  20:                     }
  21:                     else
  22:                         Roles.Add(string.Empty);
  23:                     i++;
  24:                 }
  25:             }
  26:             else
  27:                 throw new Exception("User not found in Active Directory.");
  28:  
  29:             return Roles;
  30:         }

Pada line 4 terdapat class DirectoryEntry untuk menentukan lokasi dari Lightweight Directory Access Protocol (LDAP).

Retrieving Sharepoint List Item Collection to Return List<T>

Berikut ini snippet code untuk mengambil item-item dalam Sharepoint List (SPList) berdasarakan nama field-nya dan kemudian mengembalikan item-item tersebut ke dalam bentuk collection List<T>.

   1: public List<string> GetADGroupJob()
   2:         {
   3:             List<string> listADGroupJob = new List<string>();
   4:             SPWeb web = SPContext.Current.Web;
   5:             SPList list = web.Lists["AD Group Job Name"];
   6:             SPListItemCollection collItem = list.Items;
   7:  
   8:             for (int i = 0; i < list.ItemCount; i++)
   9:             {
  10:                 listADGroupJob.Add(collItem[i].Title.ToString());
  11:             }
  12:             return listADGroupJob;
  13:         }

Semoga bermanfaat Smile

374 Views, 0 Comment(s), Published on: 12-18-2011 11:26 by dani to Microsoft and You
| More

Comments

No Comments