How to: Add Complex Rules to an Audience Using AND, OR, ( and ) Operators

Last modified: January 27, 2010

Applies to: SharePoint Server 2010

The user interface provides you with only two options for defining rules for audiences: include users that satisfy all rules, or include users that satisfy any rules. Although this might be enough for some situations, most often you will need to include users that satisfy complex rules. Complex rules use both AND and OR operators, and they may also use parentheses ( ) to group the rules to give them a different meaning.

In cases in which you need to create complex rules, you can use the Audience object model. The object model supports up to a maximum of three levels of nesting of parentheses.

The following code example adds complex rules for an audience called "John and Joe Connection". This example uses AND, OR, and ( and ) operators to combine multiple rules and to group rules.

Note Note

If you create an audience with complex rules, you cannot view or edit its properties or delete it by using the user interface. However, you can use the user interface to view its membership.

Replace servername and other strings with actual values before running the code example. Also add the following references in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.Audience;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;
using System.Collections;

namespace AudienceConsoleApp
    class Program
        static void Main(string[] args)
                using (SPSite site = new SPSite("http://servername"))
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    AudienceManager AudMgr = new AudienceManager(context);

                    AudienceCollection ac = AudMgr.Audiences;
                    Audience a = null;
                    bool ruleListNotEmpty = false;

                        a = AudMgr.Audiences["John and Joe Connection"];
                    catch (AudienceArgumentException ex)
                        //your exception handling code here

                    ArrayList aRules = a.AudienceRules;
                    if (aRules == null)
                        aRules = new ArrayList();
                        ruleListNotEmpty = true;

                        //if the rule is not emply, start with a group operator 'AND' to append
                        if (ruleListNotEmpty)
                            aRules.Add(new AudienceRuleComponent(null, "AND", null));

                        AudienceRuleComponent r0 = new AudienceRuleComponent(null, "(", null);

                        AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "John");

                        AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);

                        AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "");

                        AudienceRuleComponent r4 = new AudienceRuleComponent(null, ")", null);

                        AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);

                        AudienceRuleComponent r6 = new AudienceRuleComponent(null, "(", null);

                        AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "Joe");

                        AudienceRuleComponent r8 = new AudienceRuleComponent(null, "AND", null);

                        AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "");

                        AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
                        a.AudienceRules = aRules;
                    catch (AudienceException e)
                        //Your exception handling code here

            catch (Exception exception)