SharePoint 2013: use active directory in combination with SharePoint

this article is also used for SharePoint 2010

I have made a reference to:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.DirectoryServices.AccountManagement.dll

var users = new List<User>();

                    // set up domain context
                    var ctx = new PrincipalContext(

                    // find the group you’re interested in
                    GroupPrincipal myGroup = GroupPrincipal.FindByIdentity(ctx, userOrGroupName);

                    // if you found it – get its members
                    if (myGroup != null)
                        // if your call the GetMembers, you can optionally specify a “Recursive” flag – done here
                        PrincipalSearchResult<Principal> allMembers = myGroup.GetMembers(true);
                        foreach (UserPrincipal allMember in allMembers.Cast<UserPrincipal>().Where(allMember => allMember.EmailAddress != null))
                            user.EmailAddress = allMember.EmailAddress;
                            user.UserType = type;
                            user.Name = allMember.Name;
                            user.SamAccountName = allMember.SamAccountName;



/// <summary>
    /// The user class the bind different type of users.
    /// </summary>
    public class User
        #region Public Properties

        /// <summary>
        /// Gets or sets the email address.
        /// </summary>
        public string EmailAddress { get; set; }

        /// <summary>
        /// Gets or sets The Name property.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the sam Id.
        /// </summary>
        public string SamAccountName { get; set; }

        /// <summary>
        /// Gets or sets The user type property.
        /// </summary>
        public string UserType { get; set; }


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.