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

Write a custom Audience Field to a WebPart ToolPart for SharePoint

note: SharePoint 2013 ready, this is written for version SharePoint 2010

Audiences in SharePoint are simple to use. For example: you can make a active directory group and put the write users in this group. For example the department Finance. You can bind this group to SharePoint and its show only the people of the department finance for example. It is not a security provider but a filter system.

When you installed SharePoint and you use the audience field of a SharePoint ToolPart. It shows the WebPart when you a user in the group and hide the content when you not in the group.

The functionality what we would have is that we show other content when you are NOT in the selected audience. We choose to create another custom audience editor field on the ToolPart. You can do this with this control.

AudienceEditor ( for more information on msdn: http://msdn.microsoft.com/en-us/library/microsoft.office.server.webcontrols.audienceeditor(v=office.14).aspx )

We used this code to create the ToolPart:
//create the editor
this.audienceEditor = new AudienceEditor();
//This is a flag, when you put this three types in the editor you have the same as
//the default of SharePoint.
this.audienceEditor.Types = AudienceEditor.AudienceType.GlobalAudience | AudienceEditor.AudienceType.DL | AudienceEditor.AudienceType.SharePointGroup;

//the label for the editor field on the ToolPart.
this.audienceEditor.Text = “target Filter”

//Add this control to the ToolPart.
this.Controls.Add(this.audienceEditor);

In the WebPart file we used this code:
if (this.IfAudience())
            {
                  this.label.Text = “standard filter”;
                  base.CreateChildControls();
            }
            else
            {
                 this.label.Text = “department filter”;
             }

       /// <summary>
        /// Gets or sets specific category.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(“”)]
        public string TargetGroupFilter
        {
            get
            {
                var value = this.ViewState[“TargetGroupFilter”];

                if (value == null)
                {
                    return string.Empty;
                }

                return value.ToString();
            }

            set
            {
                this.ViewState[“TargetGroupFilter”] = value;
            }
        }

public bool IfAudience()
        {
            if (!string.IsNullOrEmpty(this.TargetGroupFilter))
            {
                if (ServerContext.Current != null)
                {
                    AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
                    return !AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, this.TargetGroupFilter, false);
                }
            }
            else
            {
                return false;
            }

            return false;
        }

Result:
audience field

The picker and the editor field.