Add Complex Rules to an Audience Using AND, OR, ( and ) Operators
The user interface provides you only two options for rules: Include users that satisfy all rules or include users that satisfy any rules. Though 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, and/or use parentheses ( ) to group the rules to give them a different meaning. Sharepoint Portal Server object model supports upto a maximum of three levels of parentheses nesting.
In such cases, you can use the Audience object model. The Audience object model allows you to create complex rules. The following code snippet adds complex rules for an audience called "SPEggheads". This example uses the AND, OR, ( and ) operators to combine multiple rules and group rules.
Note If you create an audience with complex rules, you cannot view or edit its properties or delete it using the user interface. However, you can view its membership using the user interface.
For an example of a simple rule, see Add Simple Rules to an Audience Using AND and OR Operators.
//Code snippet adds complex rules for an audience called "SPEggheads"
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
//create an audience manager object
AudienceManager AudMgr = new AudienceManager(context);
Audience a = null;
bool ruleListNotEmpty = false;
try
{
a = AudMgr.Audiences["SPEggheads"];
}
catch(AudienceArgumentException ex)
{
//your exception handling code here
}
ArrayList aRules = a.AudienceRules;
if( aRules == null )
{
aRules = new ArrayList();
}
else
{
ruleListNotEmpty = true;
}
try
{
//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);
aRules.Add(r0);
AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "a");
aRules.Add(r1);
AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r2);
AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "microsoft.com");
aRules.Add(r3);
AudienceRuleComponent r4 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r4);
AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);
aRules.Add(r5);
AudienceRuleComponent r6 = new AudienceRuleComponent(null, "(", null);
aRules.Add(r6);
AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "b");
aRules.Add(r7);
AudienceRuleComponent r8 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r8);
AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "exchange.com");
aRules.Add(r9);
AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r10);
a.AudienceRules = aRules;
a.Commit();
}
catch(AudienceException e)
{
//Your exception handling code here
}
Note If a user adds a new audience rule by calling Audience.AudienceRules.Add directly, and then calls the Commit method of the Audience class, the new rule is not saved. You must set the AudienceRules property before calling the Commit method. The recommended way to avoid this problem is to create a new ArrayList of rules and set Audience.AudienceRules = newArrayListOfRules. Setting AudienceRules in this manner sets a flag internally that indicates to Commit that the rule has changed since the last Commit. If you call Audience.AudienceRules.Add directly, this internal flag is never set and Commit does not save any changes.
Following is the list of operators supported by the operator component of the AudienceRuleComponent object.
Operator | Need left and right operands (not a group operator) |
---|---|
= | Yes |
> | Yes |
>= | Yes |
< | Yes |
<= | Yes |
Contains | Yes |
Reports Under | Yes (Left operand must be 'Everyone') |
<> | Yes |
Not contains | Yes |
AND | No |
OR | No |
( | No |
) | No |
Member Of | Yes (Left operand must be 'DL') |