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(
                        ContextType.Domain,
                        “Domain\\”,
                        “jordy.vanpaassen”,
                        “P@sswordonlyfordemo”);

                    // 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;
                            users.Add(user);
                        }
                    }

                    ctx.Dispose();

 

/// <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; }

        #endregion
    }

Advertisements

SharePoint 2013 Principal Type from a User, Active Directory or SharePoint Group

this article is also ready for SharePoint 2010 on-premise, not available on Office 365 at the moment on this way.

We would like to have a function to check what the principal type of a people picker, we made a demo code to do this.  see:

//The code block write the information about your principal type. ( SharePoint Group, SharePoint User of Active directory user )
            //This code block is only allowed in full trust SharePoint solutions. This is not ready for SharePoint Online.
            //The reason is that the SPUtility.ResolvePrincipal only for full trust solutions.
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb(site.RootWeb.ID))
                {
                    var principalInfo = SPUtility.ResolvePrincipal(web, web.CurrentUser.LoginName, SPPrincipalType.All, SPPrincipalSource.All, null, false);
                    Label label = new Label();
                    label.Text += string.Format(“Security Type: {0}”, principalInfo.PrincipalType.ToString());
                    this.Controls.Add(label);
                }
            }

the result of this code block in a simpel webpart is:

Capture

the yellow selection is the loginname this can be also the name of the sharepoint group or active directory group