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.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.